简体   繁体   中英

XAML binding list enum

   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>

I am a new guy to XAML.

I just want to display enum in listview.

But it has some problem of binding itself with binding:

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

You can use the enum with attributes and the create a list for listbox using extension methods.You can refer the below code.

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

This is simpler:

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

That's the binding that gets the items, it automatically makes the item.ToString()

and to show all the values in the DataContext, for instance:

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

In case you need a converter do the following:

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

and then in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM