开发者

wpf data binding to enable control based on multiple criteria

I've searched around, but don't think I really found an answer. I'm trying to get a handle more on d开发者_如何学编程ata binding and starting to see things coming together. Can you do data binding to something like "IsEnabled" based on TWO Properties, if so, how...

ex: A Window has some controls... certain controls may or may not be enabled at certain times. Some times it's as simple as when data is available (such as finding a record to edit), or when adding... I would consider this an "Editing" mode of the window. Sometimes, certain controls are only available when doing an Edit AND the user has admin permissions.

BOTH conditions need to be true for the control to be "enabled". Similarly could be applied to visibility of a control under similar conditions.


If you're using the MVVM model (which you really should if you're doing WPF development), then you're thinking about it the wrong way.

This sort of logic belongs in the ViewModel. You should have a single property on the ViewModel that represents the visiblity of the control (or controls) and have whatever logic is required (permissions, data validity, mode, etc.) in the ViewModel to determine this value. Putting the logic on the view hamstrings you and violates SOC.

The ViewModel is supposed to model your view. That is, there should (in most cases) be a 1:1 correlation between elements and concepts in your view (such as whether or not a feature is enabled or visible) and properties on your ViewModel.


You could use MultiBindings and some custom aggregate multi-value converters to achieve this declaratively. Alternatively, it may be more explicit (and therefore recommended) to place an additional property on your view model which compounds the values of the other view model properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜