2013-03-22 34 views
7

Hi Tôi muốn để ràng buộc một enum thiệu đến một combobox:WPF Làm thế nào để ràng buộc một enum thiệu đến một combobox

tôi đã enum tiếp theo:

public enum ReportTemplate 
    { 
    [Description("Top view")] 
    1, 
    [Description("Section view")] 
    2 
    } 

Tôi cố gắng này:

<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}" 
    x:Key="ReportTemplateEnum"> 
     <ObjectDataProvider.MethodParameters> 
      <x:Type TypeName="Helpers:ReportTemplate" /> 
     </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 

    <Style x:Key="ReportTemplateCombobox" TargetType="dxe:ComboBoxEditSettings"> 
     <Setter Property="ItemsSource" 
     Value="{Binding Source={x:Type Helpers:ReportTemplate}}"/> 
     <Setter Property="DisplayMember" Value="Description" /> 
     <Setter Property="ValueMember" Value="Value" /> 
    </Style> 

Không thể thành công để làm điều này bất kỳ 1 biết một giải pháp đơn giản?

Cảm ơn trước!

+0

Đối một cách tiếp cận thay thế, xem http://stackoverflow.com/questions/9242345/show-enum-in-a-combobox/9327548#9327548 – Phil

Trả lời

8

Điều này có thể được thực hiện bằng cách sử dụng trình chuyển đổi và mẫu mục cho comboBox của bạn.

Đây là mã chuyển đổi mà khi bị ràng buộc vào một enum sẽ trả về giá trị Mô tả:

namespace FirmwareUpdate.UI.WPF.Common.Converters 
{ 
    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; 
     } 
    } 
} 

Sau đó, trong XAML của bạn, bạn cần phải sử dụng và mẫu hàng đó.

<ComboBox Grid.Row="1" Grid.Column="1" Height="25" Width="100" Margin="5" 
       ItemsSource="{Binding Path=MyEnums}" 
       SelectedItem="{Binding Path=MySelectedItem}" 
       > 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Converter={StaticResource enumDescriptionConverter}}"/> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
+4

MyEnums và MySelectedItem là gì? – Doub

-3
public enum ReportTemplate 
{ 
[Description("Top view")] 
Top_view=1, 
[Description("Section view")] 
Section_view=2 
} 

ComboBoxEditSettings.ItemsSource = EnumHelper.GetAllValuesAndDescriptions(new 
List<ReportTemplate> { ReportTemplate.Top_view, ReportTemplate.Section_view }); 
+0

Giải thích ...? – lpapp

0

RSmaller có một câu trả lời tốt, và là một trong tôi sử dụng là tốt, với một lời cảnh báo. Nếu bạn có nhiều hơn một thuộc tính trên enums của bạn, và mô tả không phải là người đầu tiên được liệt kê sau đó phương pháp "GetEnumDescription" của ông sẽ ném một ngoại lệ ...

Đây là một phiên bản hơi an toàn hơn:

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 = null; 

      foreach(var att in attribArray) 
      { 
       if (att is DescriptionAttribute) 
        attrib = att as DescriptionAttribute; 
      } 

      if (attrib != null) 
       return attrib.Description; 

      return enumObj.ToString(); 
     } 
    } 
Các vấn đề liên quan