WPF Best Practice Exiting Windows Forms

I have extensive experience with WinForms but am new to WPF. Implementing View Design through XAML was easy to learn, but I still don't understand a bit what is expected of the MV-VM programming style. I understand the principle of separating how things look, how they behave, but in some cases this reasonably continues to elude me.

For example, if I have a keyboard with 9 buttons and I want to enable / disable all of them using the IsEnable property, the form designer in me wants to address them all in a code method targeting them by project name. What is the WPF equivalent of such an operation? Should I manage a series of bools in the codebehind and bind each one in XAML to each corresponding attribute of the button? Thanks for any recommendations. If this scenario is explained it should be enough to point me in the right direction


source to share

2 answers

This particular problem is easily solved with binding. You would bind your button properties IsEnabled

to a public property ViewModel

and based on the logic in yours ViewModel

when that property value changed. Your keyboard button will be enabled or disabled.

As @GCamel mentioned, you can also have a POCO class that will represent your button, which will implement an interface INotifyPropertyChanged

with one of the properties being the IsEnabled property. You would add instances of this class to ObservableCollection

, and when this IsEnabled property changes, your button will be enabled or disabled in the user interface.

I also highly recommend using one of the MVVM frameworks, my personal favorite is The Simple MVVM Toolkit by Tony Sned, who also has a great article on the dialog issue mentioned by @cwap Climbing aboard on the MVVM message bus



Ideally, you would have an observable collection of button_info with IsEnabled attribute, icon and text - bind the collection to whichever control is appropriate, like itemsControl, list or grid, and bind your button_info to the datatemple file ... you know what I have mean? no gui, no gui, just viewmodel and binding

or like this sample



All Articles