Under the View-Model-ViewModel pattern for WPF, I am trying to databind the Heights and Widths of various definitions for grid controls, so I can store the values the user sets them to after using a GridSplitter. However, the normal pattern doesn't seem to work for these particular properties.
Note: I'm posting this as a reference question that I'm posting as Google failed me and I had to work this out myself. My own answer to follow.
Create a
IValueConverter
as follows:You can then utilize the converter in your Binding:
Another possibility, since you brought up converting between
GridLength
andint
, is to create anIValueConverter
and use it when binding toWidth
.IValueConverter
s also handle two-way binding because they have bothConvertTo()
andConvertBack()
methods.The easiest solution is to simply use string settings for these properties so that WPF will automatically support them using GridLengthConverter without any extra work.
There were a number of gotchas I discovered:
Thusly, I used the following code:
And the XAML: