I have a list of objects of different types. Some of them are of type RadioProperty
.
Each object has some properties. The ones of interest are the following:
string PropName; // property name
string Value; // current property value
Dictionary<string, string> Values; // possible values - name, value
Value
should at any time have one of the values available in the dictionary.
What I want to do, is to somehow bind this to a radio button group, so I can select the value of the property from the available values in the dictionary.
For the moment this is what I have:
I have simplified the code for presentation purposes. Hope I didn't missed anything important.
XAML
<StackPanel>
<StackPanel.Resources>
<local:RadioPropertyConverter x:Key="radioPropertyConverter" />
</StackPanel.Resources>
<ItemsControl x:Name="PropertyList" ItemsSource="{Binding PropList}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type proptool:RadioProperty}">
<StackPanel>
<Grid>
<StackPanel>
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Path=Key}">
<!-- BEGIN: GroupName is equal to PropName -->
<RadioButton.GroupName>
<Binding Path="DataContext.PropName">
<Binding.RelativeSource>
<RelativeSource Mode="FindAncestor"
AncestorType="{x:Type TypeName=StackPanel}" />
</Binding.RelativeSource>
</Binding>
</RadioButton.GroupName>
<!-- END -->
<RadioButton.IsChecked>
<!-- specifying only `Value` for Path will fail at
runtime with the following error:
System.Windows.Markup.XamlParseException: 'A TwoWay or
OneWayToSource binding cannot work on the read-only
property 'Value' of type
'System.Collections.Generic.KeyValuePair`2[System.String,System.String]'.' -->
<Binding Converter="{StaticResource radioPropertyConverter}"
Path="/Value" ConverterParameter="Y" />
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</StackPanel>
PropList is of type List<RadioProperty>
.
The value for the ConverterParameter should be equal to the current value in the dictionary. It doesn't seem to be possible to specify a binding for the ConverterParameter:
System.Windows.Markup.XamlParseException: 'A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.'
C#
public class RadioPropertyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
I don't think that the converter should look like this but the current issue I'm having is that the Convert/ConvertBack methods are never called.
What I want to achieve is having a list of properties, each with some details in different controls. Each property might have on its own a list (Values
) which should be presented to the user as a list of radio buttons. Depending on the value of Value
a particular radio button should be selected. If a different radio button is selected, the value should change accordingly.
So I think that the binding should be done with the Value
and the radio buttons rendered depending on the dictionary's content (I already have this working).
I kindly ask you not to point me to other SO questions as I'm pretty confident that I went through most of the ones that are related to my question. Unless, of course, if you understood my question and you think that I might've missed some details in some of the already answered questions.
Some of the, presumably, more relevant SO questions, for my particular scenario, which I already went through: