【问题标题】:Binding List<string> to ComboBox将 List<string> 绑定到 ComboBox
【发布时间】:2011-11-11 21:00:51
【问题描述】:

我希望从我的列表中获取我的组合框项目名称和值 当然我不希望我的视图模型包含组合框项目列表。

我有一个列表a,b,c,d

我希望我的组合框有项目

我的视图模型得到了

public List<string> PropsList { get; set; }

我的观点得到了

<ComboBox Name="cbPropName" ItemsSource="PropsList"/>

它给了我 与 P r o p s L i s t 组合

:(

【问题讨论】:

    标签: c# wpf binding mvvm


    【解决方案1】:

    Nahum,你必须使用ItemsSource="{Binding PropsList}",假设 PropsList 是你代码后面的依赖属性,或者更好的是,你的数据上下文中实现INotifyPropertyChanged 的属性。在您的情况下,您必须将 View 的 DataContext 属性设置为 ViewModel

    【讨论】:

    • 这一直发生在我身上,即使在编写了这么多 XAML 行之后 :-)。如果对您有帮助,您可以 V 我的回答...
    • 当然 =D 只需要等待 10 分钟。这个项目中的人太快了。
    【解决方案2】:

    试试这个:

     <ComboBox Name="cbPropName" ItemsSource="{Binding Path=PropsList}" />
    

    也可以看看这个:Binding WPF ComboBox to a Custom List

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多