本文介绍了如何从其子控件访问ListViewItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自集合的 ListViewItem
,我创建了一个 DataTemplate
,以便每个 ListViewItem
都有一个 Button
作为子控件:
I have some ListViewItem
s from a collection, I have created a DataTemplate
so that each of ListViewItem
has a Button
as a child control:
<Window.Resources>
<DataTemplate x:Key="ItemTemplate_AwesomeTemplate">
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
<Button Content="Awesome Button" Click="Awesome_Button_Click" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListView x:Name="AwesomeListView" HorizontalAlignment="Left" Height="577" VerticalAlignment="Top" Width="934" ScrollViewer.HorizontalScrollBarVisibility="Visible" Foreground="Black" Margin="10,10,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="AwesomeHeader" Width="250" CellTemplate="{StaticResource ItemTemplate_AwesomeTemplate}"/>
</GridView>
</ListView.View>
</ListView>
当我单击某个 Button
时,有一种方法可以更改包含所单击的的
? ListViewItem
的 IsSelected
属性.按钮
When I click a certain Button
, is there a way to change the IsSelected
property of the ListViewItem
that contains the clicked Button
?
推荐答案
更改包含被点击的 ListViewItem
的 IsSelected
属性按钮
,您应该使用 DataContext
查找类似这样的项目:
To change the IsSelected
property of the ListViewItem
that contains the clicked Button
you should use the DataContext
to find the item like this:
private void Awesome_Button_Click(object sender, RoutedEventArgs e)
{
var item = (sender as Button).DataContext;
int index = AwesomeListView.Items.IndexOf(item);//find the index of the item that contains the clicked button
AwesomeListView.SelectedItem = AwesomeListView.Items[index];//set the AwesomeListView's SelectedItem to item that contains the clicked button
}
这篇关于如何从其子控件访问ListViewItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!