C# MVVM Light-UserControls的列表框,如何知道在哪个ItemIndex上单击了按钮
我得到了一个列表框,其中每个项目都是一个Usercontrol MatchPanel。 那个用户控件有一个按钮。 我想在单击某个项目的按钮时删除该项目。 我使用了SelectedItem,它绑定到我的ViewModel,效果很好。但有时我可以在不移动SelectedItem值的情况下单击某个项目的按钮(即使单击该项目的按钮,列表框项目也不会聚焦…)。 因此,我正在寻找一种方法,在命令CloseSelectedMatchCommand中接收一个参数,该参数将告诉我,对于我单击的按钮,它位于列表框的哪个索引处。 谢谢 这是我的看法C# MVVM Light-UserControls的列表框,如何知道在哪个ItemIndex上单击了按钮,c#,wpf,mvvm,listbox,selectedindex,C#,Wpf,Mvvm,Listbox,Selectedindex,我得到了一个列表框,其中每个项目都是一个Usercontrol MatchPanel。 那个用户控件有一个按钮。 我想在单击某个项目的按钮时删除该项目。 我使用了SelectedItem,它绑定到我的ViewModel,效果很好。但有时我可以在不移动SelectedItem值的情况下单击某个项目的按钮(即使单击该项目的按钮,列表框项目也不会聚焦…)。 因此,我正在寻找一种方法,在命令CloseSelectedMatchCommand中接收一个参数,该参数将告诉我,对于我单击的按钮,它位于列表框的
<UserControl
DataContext="{Binding ListTradingMatches, Source={StaticResource Locator}}" Height="503.175" Width="409">
<ListBox ItemsSource="{Binding Path=ListMatches}" SelectedItem="{Binding Path=SelectedMatch}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<local:MatchPanel />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的MatchPanel用户控件
<UserControl x:Class="MatchPanel"
<Label Content="Pts"/>
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBox}},
Path=DataContext.CloseSelectedMatchCommand}" CommandParameter="{Binding}">
</Button>
</Grid>
您可以将UserControl
的DataContext
设置为DataTemplate
中的项目:
<DataTemplate>
<StackPanel>
<local:MatchPanel DataContext="{Binding}" />
</StackPanel>
</DataTemplate>
…或只是整个对象:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBox}}, Path=DataContext.CloseSelectedMatchCommand}"
CommandParameter="{Binding}" />
伟大的谢里登!谢谢
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBox}}, Path=DataContext.CloseSelectedMatchCommand}"
CommandParameter="{Binding}" />