XAML绑定列表枚举

时间:2015-04-17 14:01:55

标签: c# wpf xaml enums

   class EnumToStringConverter : IValueConverter
    {

    public object Convert(object value, Type targetType, object parameter, string language)

    {
            return loai.ToDisplaytring();
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

<ListView ItemsSource="{Binding ListEnum" Margin="0,51,0,0">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=.,Converter={StaticResource EnumToStringConverter}}" HorizontalAlignment="Center" FontSize="18"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
    </ListView>

我是XAML的新人。

我只想在listview中显示枚举。

但它有一些绑定自身的问题:

{Binding Path=.,Converter={StaticResource EnumToStringConverter}}

2 个答案:

答案 0 :(得分:0)

您可以使用包含属性的枚举,并使用扩展方法为列表框创建列表。您可以参考以下代码。

    <ListBox Width="200" Height="25" ItemsSource="{Binding ComboSource}"
              DisplayMemberPath="Value"
              SelectedValuePath="Key"/>
 public class MainViewModel
{
    public List<KeyValuePair<RentStatus, string>> ComboSource { get; set; }

    public MainViewModel()
    {
        ComboSource = new List<KeyValuePair<RentStatus, string>>();
        RentStatus re=RentStatus.Active;
        ComboSource = re.GetValuesForComboBox<RentStatus>();
    }
}

public enum RentStatus
{
    [Description("Preparation description")]
    Preparation,
    [Description("Active description")]
    Active,
    [Description("Rented to people")]
    Rented
}

public static class ExtensionMethods
{       
    public static List<KeyValuePair<T, string>> GetValuesForComboBox<T>(this Enum theEnum)
    {
        List<KeyValuePair<T, string>> _comboBoxItemSource = null;
        if (_comboBoxItemSource == null)
        {
            _comboBoxItemSource = new List<KeyValuePair<T, string>>();
            foreach (T level in Enum.GetValues(typeof(T)))
            {
                string Description = string.Empty;                    
                FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);                    
                if (attributes != null && attributes.Length > 0)
                {
                    Description = attributes.FirstOrDefault().Description;
                }
                KeyValuePair<T, string> TypeKeyValue = new KeyValuePair<T, string>(level, Description);
                _comboBoxItemSource.Add(TypeKeyValue);
            }
        }
        return _comboBoxItemSource;
    }
}

答案 1 :(得分:0)

这更简单:

 <ListView x:Name="ListViewInstance" ItemsSource="{Binding ListEnum}" Margin="0,51,0,0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" HorizontalAlignment="Center" FontSize="18"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这是获取项目的绑定,它会自动生成item.ToString()

并显示DataContext中的所有值,例如:

public List<Devices> ListEnum {  get { return typeof(Devices).GetEnumValues().Cast<Devices>().ToList(); } }

如果您需要转换器,请执行以下操作:

 public class EnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return what you need to convert from value
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后在XAML中

<Window.Resources>
    <local:EnumToStringConverter  x:Key="EnumToStringConverter"/>
</Window.Resources>

<ListView x:Name="ListViewInstance" ItemsSource="{Binding ListEnum}" Margin="0,51,0,0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding, Converter={StaticResource EnumToStringConverter}}" HorizontalAlignment="Center" FontSize="18"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>