È possibile utilizzare il metodo ObjectDataProvider per associare un ListBox a un enum e formattarlo in qualche modo per visualizzare l'Attriibute descrizione? Se sì, come si fa a fare questo ...?WPF Associazione di un ListBox a un enum, visualizzazione dell'attributo Descrizione
risposta
Sì, è possibile. Questo lo farà. Dire che abbiamo enum
public enum MyEnum
{
[Description("MyEnum1 Description")]
MyEnum1,
[Description("MyEnum2 Description")]
MyEnum2,
[Description("MyEnum3 Description")]
MyEnum3
}
Poi possiamo usare l'ObjectDataProvider come
xmlns:MyEnumerations="clr-namespace:MyEnumerations"
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="MyEnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="MyEnumerations:MyEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
E per la ListBox abbiamo impostato l'ItemsSource per MyEnumValues e applicare un ItemTemplate con un convertitore.
<ListBox Name="c_myListBox" SelectedIndex="0" Margin="8"
ItemsSource="{Binding Source={StaticResource MyEnumValues}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
E nel convertitore si ottiene la descrizione e restituirlo
public class EnumDescriptionConverter : IValueConverter
{
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return enumObj.ToString();
}
else
{
DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
return attrib.Description;
}
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Enum myEnum = (Enum)value;
string description = GetEnumDescription(myEnum);
return description;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Empty;
}
}
Il metodo GetEnumDescription dovrebbe probabilmente andare altrove, ma si ottiene l'idea :)
Se si esegue il binding all'Enum, è possibile convertirlo alla descrizione tramite un IValueConverter.
Vedere Binding ComboBoxes to enums... in Silverlight! per una descrizione su come eseguire ciò.
Vedere http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx per ulteriori informazioni.
È possibile definire un file ressource nel tuo progetto (* .resx file). In questo file è necessario definire "chiave-valore-coppie", qualcosa di simile:
"YellowCars" : "Yellow Cars",
"RedCars" : "Red Cars",
e così via ...
Le chiavi sono uguali ai tuoi enum-voci, qualcosa di simile:
public enum CarColors
{
YellowCars,
RedCars
}
e così via ...
Quando si utilizza WPF è possibile implementare nel vostro XAML-Code, qualcosa di simile:
<ComboBox ItemsSource="{Binding Source={StaticResource CarColors}}" SelectedValue="{Binding CarColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource CarColorConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
allora si deve scrivere il convertitore, qualcosa di simile:
using System;
using System.Globalization;
using System.Resources;
using System.Windows.Data;
public class CarColorConverter : IValueConverter
{
private static ResourceManager CarColors = new ResourceManager(typeof(Properties.CarColors));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var key = ((Enum)value).ToString();
var result = CarColors.GetString(key);
if (result == null) {
result = key;
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
La mia risposta arriva 7 anni a fine ;-) Ma forse può essere utilizzato da qualcun altro!
Possibile duplicato di http://stackoverflow.com/questions/1281490/binding-comboboxes-to-enums-in-silverlight. –