【问题标题】:Binding Enum[] to ListBox将 Enum[] 绑定到 ListBox
【发布时间】:2010-05-20 06:05:59
【问题描述】:

我有下一个枚举

Enum rcCategory
{
  Incoming,
  Internal,
  Outgoing
}

我的班级中有 rcCategory[] 类型的属性“类别”。

我想将此属性绑定到列表框。我为此使用下一个代码

MyListBox.SetBinding (ListBox.ItemsSource, new Binding {Source= myClass.categories});

但是这段代码没有按预期工作。 我怎样才能做到这一点。我的 listBox 始终为空,但源属性具有值

【问题讨论】:

    标签: wpf binding enums


    【解决方案1】:

    请参阅 Bea Stollnitz 排名靠前的文章。
    简而言之,您需要绑定到调用静态方法 Enum.GetValues( typeof(YourEnum) ) 的 ObjectProvider 以返回列表。

    http://bea.stollnitz.com/blog/?p=28

    更新:抱歉遇到了一点速读问题。这个更容易..验证它有效。推荐:找到一份 ProgrammingWPF 的副本,并通过 DataBinding 章节...

    XAML:

    <ListBox DockPanel.Dock="Left" ItemsSource="{Binding EnumArrayProp}"/>
    

    代码隐藏:

    public partial class Window1 : Window
       {
           public rcCategory[] EnumArrayProp
           {
               get; set;
           }
           public Window1()
           {
               InitializeComponent();
    
               this.EnumArrayProp = new rcCategory[] { rcCategory.Incoming, rcCategory.Incoming, rcCategory.Outgoing };
    
               this.DataContext = this;
    
           }
    

    【讨论】:

    • 我读了这篇文章。但在这个将简单枚举绑定到 ListBox 的唯一示例中。我的 enum[] 问题不是简单的枚举
    猜你喜欢
    • 2012-01-26
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多