Binding to a
Tag
is not how I would do it.
But, as this is how you want to, will need to write an
IValueConverter[
^] to work with the
Tag
property:
public class CharConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((char)value) switch
{
'M' => "Male",
'F' => "Female",
_ => "Other"
};
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((ComboBoxItem)value).Tag;
}
}
and to use:
<Grid>
<Grid.Resources>
<local:CharConverter x:Key="CharConverter" />
</Grid.Resources>
<ComboBox SelectedItem="{Binding Gender,
Converter={StaticResource CharConverter}}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="100">
<ComboBoxItem Content="Male" Tag="M"/>
<ComboBoxItem Content="Female" Tag="F"/>
<ComboBoxItem Content="Other" Tag="O"/>
</ComboBox>
</Grid>