I'm trying to express an enumeration property in my view-model as a set of radio buttons in my view. So far, so good; I can express that with a two-way MultiBinding
:
(rb1.IsChecked, rb2.IsChecked, rb3.IsChecked) <-> vm.Value
The multi-binding used here would feature a multi-converter that converts between (bool, bool, bool) <-> MyValue
; obviously, one of the (three) allowable values of the MyValue
type is chosen based on which bool
is true
, and vice-versa.
This is already a bit inconvenient, though: I cannot define that binding in my view's Xaml, as multi-bindings have to be defined from the side of the single value. Hence, I have to define the multi-binding in code-behind and use SetBinding
set it on my view model's Value
property.
Now, the issue that I'm stuck at is that I'm not just binding one set of radio buttons to that value, but two. Hence, my bindings would have to look like this:
(rbA1.IsChecked, rbA2.IsChecked, rbA3.IsChecked) <-> vm.Value <-> (rbB1.IsChecked, rbB2.IsChecked, rbB3.IsChecked)
The problem is that I cannot use SetBinding
to connect several bindings to vm.Value
at a time.
Solutions that I have tried so far are:
- Use one big multi-binding, binding to all radio buttons at a time. This would mean a binding of the form
(rbA1.IsChecked, rbA2.IsChecked, rbA3.IsChecked, rbB1.IsChecked, rbB2.IsChecked, rbB3.IsChecked) <-> vm.Value
. The problem with this solution is that if one of the radio buttons (say,rbB2
) is checked, I have no way of telling whetherrbA2
(unchecked) orrbB2
(checked) has the "new, correct" value. - Abstract the radio button groups first by defining a radio group control that exposes only one
SelectedIndex
property. This property can then be conveniently bound to myvm.Value
property from all instances of my radio group control. While feasible, it requires writing a new control class and I wonder whether this is the only way in WPF. - Bind one set of radio buttons to another one: By two-way-binding
rbB1
torbA1
,rbB2
torbA2
, and so on, and using a multi-binding between myvm.Value
and the first set of radio buttons only, I would achieve the desired effect, but I don't like the notion of having a "master radio group". It would be abusing GUI elements for data transfer. - Do everything in code-behind and manually update radio buttons and view-model value. Of course this is a viable fallback solution, but this feels like it should be feasible in Xaml/with bindings.