WPF MVVM-访问视图模型中视图控件的属性

问题描述:

我在视图中使用了不同的切换按钮和按钮.我需要使用视图模型中的 toggleButtons 的 IsEnabled 和 IsChecked 属性来根据某些条件禁用某些按钮和切换按钮.向我建议任何方法来访问视图中控件的任何属性到视图模型.

I am using different toggle buttons and buttons in the view. I need to use the IsEnabled and IsChecked property of the toggleButtons in the view Model to diable some buttons and toggle buttons based on some conditions. Suggest me any methods to access any properties of a control in the view to the view model.

将它们绑定到 View Model 中公开的属性,这样您就不会打破 View 和 View Model 之间的分离.

Bind them to exposed properties in the View Model so you don't break the separation between View and View Model.

在这种情况下,您可以将IsEnabled"绑定到SomethingIsEnabled",然后在设置后,根据属性当前值重新评估您的命令状态.

In this case, you can bind the "IsEnabled" to a "SomethingIsEnabled" and then when that is set, re-evaluate your command status based on the properties current value.

我注意到对于视图模型中的状态,我最终创建了很多这样的东西来保持视图模型中的逻辑",而不是读取视图.

I have noticed that for state within the View Model, I end up creating a lot of things like this to keep the "logic" in the View Model, instead of reading the View.