如何在 wp7 中更改列表框项的可见性属性? [英] How can I change listbox item's visibility property in wp7?

查看:32
本文介绍了如何在 wp7 中更改列表框项的可见性属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:有一个列表框:

<ListBox Margin="24,107,12,28" Name="lb">
  <TextBlock Text="Text" TextWrapping="Wrap" FontSize="22" Visibility="Collapsed" />
  <TextBlock Text="Text2" TextWrapping="Wrap" FontSize="22" Visibility="Collapsed" />
  <TextBlock Text="Text3" TextWrapping="Wrap" FontSize="22" Visibility="Collapsed" />
</ListBox>

如何以编程方式更改 TextBlocks 的可见性属性?

How can I change TextBlocks's visibility properties programmatically?

推荐答案

ListBox 为每个项目生成一个 ListBoxItem 类型的容器.您可以通过以下方式访问它:

The ListBoxgenerates a container of type ListBoxItem for each item. You can access it as follows:

ListBoxItem lbi = lb.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem
lbi.Visibility = Visibility.Collapsed;

如果您想访问 TextBlock,您需要导航 ListBoxItem 的可视化树.例如,使用 Linq to VisualTree:

If you want access to the TextBlock you will need to navigate the visual tree of the ListBoxItem. For example, using Linq to VisualTree:

TextBlock txt = lbi.Descendants<TextBlock>().Single() as TextBlock;

这篇关于如何在 wp7 中更改列表框项的可见性属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆