wpf combobox绑定到enum
搜索到这个网页
在其中发现了这个方法,
比较简单:
public enum RULE { [Description( "Любые, без ограничений" )] any, [Description( "Любые если будет три в ряд" )] anyThree, [Description( "Соседние, без ограничений" )] nearAny, [Description( "Соседние если будет три в ряд" )] nearThree } class ExtendRULE { public static object Values { get { List<object> list = new List<object>(); foreach( RULE rule in Enum.GetValues( typeof( RULE ) ) ) { string desc = rule.GetType().GetMember( rule.ToString() )[0].GetCustomAttribute<DescriptionAttribute>().Description; list.Add( new { value = rule, desc = desc } ); } return list; } } }
<StackPanel> <ListBox ItemsSource= "{Binding Source={x:Static model:ExtendRULE.Values}}" DisplayMemberPath="desc" SelectedValuePath="value" SelectedValue="{Binding SelectedRule}"/> <ComboBox ItemsSource="{Binding Source={x:Static model:ExtendRULE.Values}}" DisplayMemberPath="desc" SelectedValuePath="value" SelectedValue="{Binding SelectedRule}"/> </StackPanel>
照猫画虎,
实现了自己的需求。