WPF CommandParameter RelativeSource绑定 [英] WPF CommandParameter RelativeSource Binding
问题描述
我在ListView的DataTemple中有一个带CheckBox的ListView。向我展示了如何使Command正常工作。我想捕获ListView SelectedItem作为参数传递给Command,但是我不正确...
I have a ListView with a CheckBox inside of the ListView's DataTemple. I was shown how to make the Command work. I would like to capture the ListView SelectedItem to pass as a parameter to the Command, but I don't have it right...
<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>
推荐答案
再次查看问题,我认为我理解正确现在。
这是从 ListView
中获取 SelectedItem
的另一种方法,然后在 CheckBox
我将 CommandParameter
绑定如下
Looking at the problem again I think I understood it correctly now.
Here is a different approach to get the SelectedItem
from the ListView
Then in the CheckBox
I bound the CommandParameter
as below
CommandParameter = {Binding ElementName = lvReferralSource,Path = SelectedItem}
以下将传递与<$相关的对象c $ c> CheckBox
CommandParameter = {Binding} // ListView中的完整对象
在 Command方法
中与 CheckBox $ c相关$ c>可以将参数对象转换为正确的类型(
ListView ItemSource
中对象的类型),并获取 Value的值
和活动
In the Command Method
related to the CheckBox
you can cast the parameter object to the correct type(type of the objects in the ListView ItemSource
) and get the value of Value
and Active
这篇关于WPF CommandParameter RelativeSource绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!