简体   繁体   English

如何将ItemTemplate CheckBox的Command属性绑定到ViewModel对象的属性?

[英]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.

相关问题 如何将对象的布尔属性绑定到CheckBox的IsChecked属性? - How to bind an object's boolean property to a CheckBox's IsChecked property? 绑定到资源对象中的ViewModel属性 - Bind to ViewModel property in resource object 如何将itemtemplate中的属性绑定到itemsource中的一个属性? - How to bind a property in itemtemplate to a proptery in itemsource? 如何将 WPF 复选框的 IsChecked 属性绑定到非窗口对象的布尔属性 - How can I bind a WPF checkbox's IsChecked property to a boolean property of an object that is not a window WinUI 3 使用 DataTemplate 时如何将命令绑定到 ViewModel 属性? - WinUI 3 How to Bind Command to ViewModel property when using a DataTemplate? 如何绑定到视图中的属性而不绑定到ViewModel中? - How to bind to a property in view but not in viewmodel? 如何将嵌套元素的属性绑定到 viewmodel 属性 - How to bind the property of nested element to the viewmodel property 如何将ViewModel属性绑定到转换器中的依赖项属性 - How to bind ViewModel Property to the dependency property in the convertor 如何将UserControl的DependencyProperty绑定到MVVM的ViewModel中的属性? - How to bind a DependencyProperty of a UserControl to a property in it's ViewModel in MVVM? 在ViewModel WPF中将对象从按钮绑定到属性 - Bind Object from Button to Property in ViewModel WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM