如何将枚举设置为xaml中的列表框。但是在列表框中我需要显示描述而不是枚举的名称/值。然后,当我单击一个按钮时,我需要将选定的枚举通过icommand作为枚举而不是字符串传递给方法。 例如:
public enum MyEnum
{
EnumOne = 0,
[Description("Enum One")]
EnumTwo = 1,
[Description("Enum Two")]
EnumTwo = 2,
[Description("Enum Three")]
}
需要将这些枚举绑定到具有描述的displaymemberpath的列表框。然后在列表框中进行选择后,传递所选的枚举,如下所示:
private void ButtonDidClick(MyEnum enum)
{
}
XAML:
<ListBox ItemsSource="{Binding MyEnum"} /> ?
我知道如何将命令连接到按钮等等。感谢您的帮助。
答案 0 :(得分:3)
来自制作应用程序
很久以前得到了这个,无法找到来源
将DisplayMememberPath绑定到值
public static Dictionary<T, string> EnumToDictionary<T>()
where T : struct
{
Type enumType = typeof(T);
// Can't use generic type constraints on value types,
// so have to do check like this
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
Dictionary<T, string> enumDL = new Dictionary<T, string>();
foreach (T val in Enum.GetValues(enumType))
{
enumDL.Add(val, val.ToString());
}
return enumDL;
}
对于那些想知道如何阅读description属性值的人。以下内容可以轻松转换为使用enum
或扩展。我发现这种实现更灵活。
使用此方法,将val.ToString()
替换为GetDescription(val)
。
/// <summary>
/// Returns the value of the 'Description' attribute; otherwise, returns null.
/// </summary>
public static string GetDescription(object value)
{
string sResult = null;
FieldInfo oFieldInfo = value.GetType().GetField(value.ToString());
if (oFieldInfo != null)
{
object[] oCustomAttributes = oFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
if ((oCustomAttributes != null) && (oCustomAttributes.Length > 0))
{
sResult = ((DescriptionAttribute)oCustomAttributes[0]).Description;
}
}
return sResult;
}
答案 1 :(得分:3)
使用ObjectDataProvider:
<ObjectDataProvider x:Key="enumValues"
MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:ExampleEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
然后绑定到静态资源:
ItemsSource="{Binding Source={StaticResource enumValues}}"
找到此解决方案here
答案 2 :(得分:1)
您可以通过将Enum转换为MyEnum-string元组列表并使用ListBox的DisplayMemberPath参数显示描述项来实现。当你选择一个特定的元组时,只需抓住它的MyEnum部分,并使用它来设置ViewModel中的SelectedEnumValue属性。
以下是代码:
XAML:
<Window x:Class="EnumToListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0"
ItemsSource="{Binding EnumToDescriptions}"
SelectedItem="{Binding SelectedEnumToDescription}"
DisplayMemberPath="Item2"/>
<TextBlock Grid.Row="1"
Text="{Binding SelectedEnumToDescription.Item2}"/>
</Grid>
守则背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
public class ViewModel : PropertyChangedNotifier
{
private List<Tuple<MyEnum, string>> _enumToDescriptions = new List<Tuple<MyEnum, string>>();
private Tuple<MyEnum, string> _selectedEnumToDescription;
public ViewModel()
{
Array Values = Enum.GetValues(typeof(MyEnum));
foreach (var Value in Values)
{
var attributes = Value.GetType().GetField(Value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
var attribute = attributes[0] as DescriptionAttribute;
_enumToDescriptions.Add(new Tuple<MyEnum, string>((MyEnum)Value, (string)attribute.Description));
}
}
public List<Tuple<MyEnum, string>> EnumToDescriptions
{
get
{
return _enumToDescriptions;
}
set
{
_enumToDescriptions = value;
OnPropertyChanged("EnumToDescriptions");
}
}
public Tuple<MyEnum, string> SelectedEnumToDescription
{
get
{
return _selectedEnumToDescription;
}
set
{
_selectedEnumToDescription = value;
SelectedEnumValue = _selectedEnumToDescription.Item1;
OnPropertyChanged("SelectedEnumToDescription");
}
}
private MyEnum? _selectedEnumValue;
public MyEnum? SelectedEnumValue
{
get
{
return _selectedEnumValue;
}
set
{
_selectedEnumValue = value;
OnPropertyChanged("SelectedEnumValue");
}
}
}
public enum MyEnum
{
[Description("Item1Description")]
Item1,
[Description("Item2Description")]
Item2,
[Description("Item3Description")]
Item3,
[Description("Item4Description")]
Item4
}
public class PropertyChangedNotifier : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}