J'ai un simple ComboBox
qui a quelques valeurs simples. J'essaie de faire une liaison bidirectionnelle avec une propriété enum sur mon modèle.WPF ComboBox Liaison bidirectionnelle
<ComboBox d:LayoutOverrides="Height" Grid.Column="1" SelectedItem="{Binding SortType, Converter={StaticResource sortSelect}, Mode=TwoWay}">
<ListBoxItem Content="Ascending" Tag="Ascending"/>
<ListBoxItem Content="Descending" Tag="Descending"/>
<ListBoxItem Content="Absolute Ascending" Tag="AbsoluteAscending"/>
<ListBoxItem Content="Absolute Descending" Tag="AbsoluteDescending" />
</ComboBox>
Voici mon ValueConverter
public class RdiSortMatchConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = (RdiSort) value;
switch (val)
{
case RdiSort.Ascending:
return "Ascending";
case RdiSort.Descending:
return "Descending";
case RdiSort.AbsoluteAscending:
return "Absolute Ascending";
case RdiSort.AbsoluteDescending:
return "Absolute Descending";
default:
throw new ArgumentOutOfRangeException();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (RdiSort) Enum.Parse(typeof (RdiSort), (string) ((ListBoxItem) value).Tag);
}
}
La méthode ConvertBack
fonctionne très bien, et met à jour mon modèle basé sur la valeur Tag
dans le ListBoxItem
, mais je ne peux pas obtenir la valeur Enum
initiale pour sélectionner la ListBoxItem
correcte
quelle est la meilleure façon d'y parvenir, ou existe-t-il un meilleur moyen de lier t Enums
(prendre en compte que j'ai besoin d Descriptions pour chaque valeur Enum
.
Voir cette question: [Databinding une propriété ENUM à un ComboBox dans WPF] (http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf) –