如何得到ItemsControl下面的控件们?
<ItemsControl x:Name="playListBox" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border CornerRadius="2" Margin="2" BorderBrush="#77ffffff" BorderThickness="1">
<Grid x:Name="itemgrid" MouseLeftButtonUp="itemgrid_MouseLeftButtonUp">
<TextBlock x:Name="title"/>
<TextBlock x:Name="id" Text="{binding id}" Visibility="Collapsed"/>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
private void itemgrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
UIElementCollection uic = (sender as Grid).Children;
foreach (UIElement ui in uic)
{
if (ui.GetType() == typeof(TextBlock))
{
if ((ui as TextBlock).Name.Equals("thisid"))
{
UIElementCollection uic = (sender as Grid).Children;
foreach (UIElement ui in uic)
{
if (ui.GetType() == typeof(TextBlock))
{
if ((ui as TextBlock).Name.Equals("title"))
{
(ui as TextBlock).Foreground = new SolidColorBrush(Colors.Green);
}
}
}
}
}
}
}
我通过MouseLeftButtonUp可以取到id的值从而知道这是点击的第几个项目,再进行一些设置等。
现在我想知道怎么能不通过点击直接取到itemscontrol中的第n个项目,然后再进行设置
[解决办法]
Grid grid= base.GetTemplateChild("itemgrid") as Grid ; //通过这个方法获取ControlTemplate外观元素
再执行你上面的循环操作