积极答复者
如何取得ListView 中,某一条的template 信息

问题
-
是这样的过程:
1. 定义了一个 Listview 的每个Item的MessageListImageTemplate,格式包括:Rectangle、TextBlock、Image
2.设置 ListView 的 template 为MessageListImageTemplate:
ItemTemplate
="{StaticResourceMessageListImageTemplate}"
3. 我希望在ListView 的
ItemListView_SelectionChanged 函数中,得到选中行的template。然后template->Rectangle->Visibility = Collapsed!
这个过程该如何实现呢??
答案
-
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[1]);
TextBlock textblock = (TextBlock)FindVisualChildByName(ListBoxItemObj, "textBlock");
textblock.Foreground = new SolidColorBrush(Colors.Red);
}
public FrameworkElement FindVisualChildByName(DependencyObject obj, string name)
{
FrameworkElement ret = null;
int numChildren = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < numChildren; i++)
{
var objChild = VisualTreeHelper.GetChild(obj, i);
var child = objChild as FrameworkElement;
if (child != null && child.Name == name)
{
return child;
}ret = FindVisualChildByName(objChild, name);
if (ret != null)
break;
}
return ret;
}Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Min ZhuModerator 2012年10月9日 7:33
全部回复
-
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[1]);
TextBlock textblock = (TextBlock)FindVisualChildByName(ListBoxItemObj, "textBlock");
textblock.Foreground = new SolidColorBrush(Colors.Red);
}
public FrameworkElement FindVisualChildByName(DependencyObject obj, string name)
{
FrameworkElement ret = null;
int numChildren = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < numChildren; i++)
{
var objChild = VisualTreeHelper.GetChild(obj, i);
var child = objChild as FrameworkElement;
if (child != null && child.Name == name)
{
return child;
}ret = FindVisualChildByName(objChild, name);
if (ret != null)
break;
}
return ret;
}Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Min ZhuModerator 2012年10月9日 7:33