我在ListView的DataTemple中有一个带有CheckBox的ListView。他们向我展示了如何使命令工作。我想要捕获ListView SelectedItem作为参数传递给命令,但我没有这样做……
<ListView x:Name="lvReferralSource" ItemsSource="{Binding ReferralObsCollection}" Style="{StaticResource TypeListViewStyle}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="ckbReferralIsChecked" Content="{Binding Value}" IsChecked="{Binding Active}" Style="{StaticResource CheckBoxStyleBase2}"
Command="{Binding DataContext.CheckBoxIsChecked, RelativeSource={RelativeSource AncestorType=ListView}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=SelectedItem}">
</CheckBox>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>发布于 2019-06-04 14:34:50
再看一遍这个问题,我想我现在理解正确了。下面是从ListView获取SelectedItem的另一种方法,然后在CheckBox中绑定CommandParameter,如下所示
CommandParameter="{Binding ElementName=lvReferralSource, Path=SelectedItem}"
下面的代码将传递与CheckBox相关的对象
CommandParameter="{Binding}"// Full object from the ListView
在与CheckBox相关的Command Method中,您可以将参数object转换为正确的类型(ListView ItemSource中对象的类型),并获得Value和Active的值
https://stackoverflow.com/questions/56435313
复制相似问题