[英]How to bind the Command property of the ItemTemplate CheckBox to ViewModel object's property?
Let me ask this question with a pseudo code: 让我用伪代码问这个问题:
<Window>
<ListView ItemsSource="{Binding PersonCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Age}" />
<TextBlock Text="/" />
<CheckBox Command="{Binding PersonSelectedCommand}" /> <!-- Where "PersonSelectedCommand" is a public command property available in ViewModel object (lets say "Contacts" in this context)-->
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Window>
Where 哪里
"Contacts" the ViewModel object set as the DataContext for the window. 将ViewModel对象“联系”设置为窗口的DataContext。
"Contacts" has "PersonCollection" , public ICommand PersonSelectedCommand properties. “联系人”具有“ PersonCollection”(公共ICommand PersonSelectedCommand属性)。 "PersonCollection" is List
“ PersonCollection”是列表
"Person" has Name, Age properties “人”具有“姓名”,“年龄”属性
Currently this is not working as CheckBox is trying to find and bind the ICommand "PersonSelectedCommand" property of object "person", which does not exists! 当前,此功能不起作用,因为CheckBox试图查找并绑定对象“ person”的ICommand“ PersonSelectedCommand”属性,该属性不存在!
How will bind the CheckBox to the ICommand "PersonSelectedCommand" property of object "Contact" 如何将CheckBox绑定到对象“ Contact”的ICommand“ PersonSelectedCommand”属性
Thanks and regards 谢谢并恭祝安康
123Deveopler 第123章
I liked SeeSharp's answer, but to directly answer your question, all you need to do is change your CheckBox's Command binding to: 我喜欢SeeSharp的回答,但要直接回答您的问题,只需将CheckBox的Command绑定更改为:
Command="{Binding DataContext.PersonSelectedCommand,
RelativeSource={RelativeSource FindAncestor,ListView,1}}"
This is preferable to SeeSharp's answer only when you need more control than simply binding the IsSelected property will give you. 仅当您需要更多控制权而不是简单地绑定IsSelected属性会给您带来更多控制权时,这才比SeeSharp的答案更好。 Otherwise go with binding IsSelected.
否则,请绑定IsSelected。
Can you change view model? 您可以更改视图模型吗? I think will be better, if you add bool property IsSelected to Person.
我认为如果将bool属性IsSelected添加到Person会更好。 And bind it to checkbox:
并将其绑定到复选框:
<CheckBox IsChecked="{Binding IsSelected}"/>
Command is not requared and you can add some functionality in setter of property IsSelected. 命令不会重新排序,您可以在属性IsSelected的设置器中添加一些功能。
The PersonSelectedCommand
has to be in the Person scope. PersonSelectedCommand
必须在Person范围内。 So you'll have a list of commands when you bind to a list of persons. 因此,当绑定到人员列表时,将具有命令列表。 Hence whenever a person is selected, you will have the corresponding command to be executed.
因此,无论何时选择一个人,都将执行相应的命令。
Else you can find out the Ancestor using RelativeSource in Binding and set the PersonSelectedCommand that way. 否则,您可以在Binding中使用RelativeSource找出祖先,并以这种方式设置PersonSelectedCommand。 Check this answer: Is there a simple way to specify a WPF databinding where the path is one "level" up?
检查以下答案: 是否存在一种简单的方法来指定WPF数据绑定,而路径是向上的“一级”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.