Ramblings of a .NET Developer

22 September 2014

Monday, September 22, 2014 | by Paul | Categories: , , | No comments
So, it's been about 6 years since I last did any winforms development, but my current contract requires me to develop some applications using WinForms whilst others can be done in WPF and yet more stuff is web development. So, I've got quite a bit going on, which is great as I need to brush up on stuff other than WPF.

I didn't develop Clarity with the view of using it in WinForms, but decided to use it anyway as it was a good exercise in testing it's compatibility.

As it turned out, I didn't have to change too much. I introduced a new CommandBuilder interface and implementations in both WPF and WinForms. I've also introduced a Clarity.WinForms library which is akin to Clarity.WPF to allow people to get more features out of the box.

In Clarity.WinForms, I created a couple of extenders to allow developers identify bindings in the designer. These are PropertyBinding and CommandBinding. The PropertyBinding allows you to identify the property in the ViewModel a control should be bound to. Likewise, the CommandBinding allows you identify the IClarityCommand to execute when a button is clicked.

I've also added 3 additional example applications.

The first is a WinForms earnings calculator. It's a simple one window application that will calculate the amount you've earned that day.

Here is a screenshot of the designer.

This shows the Income control selected, and the PropertyName highlighted on the Properties window. This is available because I've included the PropertyBinding extender. For this sample, I'm binding it to the IncomeAmount property which is on the Income property of the ViewModel.

Normally, for an application like this you would see click event handlers for the buttons, code to validate the data, and code to set the values on a ViewModel or Model.

In the world of Clarity, most of the code is removed, with only a binding added for the ComboBox.


As all UI components in Clarity, this is a UserControl, but instead of inheriting from UserControl, we inherit from Clarity.Winforms.View. This provides helpers for binding controls and handles where the ViewModel has not yet been created.

Once a ViewModel has been associated with the View, the BindViewModel virtual method is called.

We override it here, to allow us to bind the ComboBox to a datasouce provided by the ViewModel (FrequencyList), the selected value property "Income.Frequency" and an optional Converter, in our case I'm using an EnumConverter as the Frequency property is an enum.

Running the sample should show the UI, bound to a ViewModel and being updated from the ViewModel via an internal timer.

To test validation, tick the "Make Form Invalid If Frequency is Day" then select the Day option in the Frequency dropdown. This should make the form invalid and show the good old WinForms validator error icon.



I've also created a more comprehensive sample in the FileExplorer folder. This shows a common ViewModel library being consumed by WPF and WinForms UI versions. The pain point with this was the TreeView binding. To help, I've created a BindableTreeView control in Clarity.Winforms that helps with the sub-tree bindings.

This WinForms part of the library is still work in progress but hopefully this is a reasonable start.

0 comments:

Post a Comment