2010-10-21 37 views
30

Có thể sử dụng phương pháp ObjectDataProvider để ràng buộc một ListBox vào một enum, và phong cách nó bằng cách nào đó để hiển thị mô tả attriibute? Nếu vậy thì làm thế nào một người sẽ làm được điều này ...?WPF Ràng buộc một ListBox vào một enum, hiển thị các thuộc tính mô tả

+0

Bản sao có thể có của http://stackoverflow.com/questions/1281490/binding-comboboxes-to-enums-in-silverlight. –

Trả lời

79

Có, điều đó là có thể. Điều này sẽ làm điều đó. Giả sử chúng ta có enum

public enum MyEnum 
{ 
    [Description("MyEnum1 Description")] 
    MyEnum1, 
    [Description("MyEnum2 Description")] 
    MyEnum2, 
    [Description("MyEnum3 Description")] 
    MyEnum3 
} 

Sau đó chúng ta có thể sử dụng ObjectDataProvider như

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> 

Và đối với ListBox chúng tôi đặt ItemsSource để MyEnumValues ​​và áp dụng một ItemTemplate với một chuyển đổi.

<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> 

Và trong bộ chuyển đổi chúng tôi nhận được mô tả và gửi lại

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; 
    } 
} 

Phương pháp GetEnumDescription có lẽ nên đi ở một nơi khác nhưng bạn sẽ có được ý tưởng :)

+0

Xin cảm ơn, sẽ cho phép thử ngay bây giờ :) –

+4

Yêu thích nó, gank nó. Tôi sử dụng một chút LINQ để ghép nối GetEnumDescription, bạn có thể snag nó ở đây http://pastebin.com/XLm9hbhG – Will

+2

Vì vậy, bạn phải thực hiện một công cụ chuyển đổi cho mỗi loại enum? – Carlo

0

Bạn có thể định nghĩa một tập tin ressource trong dự án của bạn (* .resx file). Trong tập tin này, bạn phải xác định "giá trị khóa cặp", một cái gì đó như thế này:

"YellowCars" : "Yellow Cars", 
"RedCars" : "Red Cars", 

và vân vân ...

Các phím được tương đương với enum-mục của bạn, một cái gì đó như thế này:

public enum CarColors 
{ 
    YellowCars, 
    RedCars 
} 

và vân vân ...

Khi bạn sử dụng WPF bạn có thể thực hiện trong bạn XAML-Mã, một cái gì đó như thế này:

<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> 

Sau đó, bạn phải viết Chuyển đổi của bạn, một cái gì đó như thế này:

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(); 
    } 
} 

câu trả lời của tôi đến 7 năm đến cuối ;-) Nhưng có lẽ nó có thể được sử dụng bởi người khác!

Các vấn đề liên quan