【问题标题】:Binding to the ItemSource绑定到 ItemSsource
【发布时间】:2016-03-15 10:21:32
【问题描述】:

我正在尝试动态构建 AutoCompleteBox。然后我想在我的 itemsource 中使用客户列表,并且用户可视化 DescClient 字段。

List<Client> clients = ClientAction.getClientsFromUsers(Environment.UserName);
(field as AutoCompleteBox).ItemsSource = clients;

//CLIENT MEMBERS
    private int idClient;
            private string descClient;
            private int idGroup;
            private User user;

已经尝试过:

(field as AutoCompleteBox).ValueMemberPath = "DescClient"

但没有显示任何内容。我做错了什么会影响 ValueMemberPath 还是应该使用另一个 Proprety?

【问题讨论】:

  • 您是否为客户成员定义了属性?
  • 请注意,最好写((AutoCompleteBox)field) 而不是(field as AutoCompleteBox),因为如果field 不是AutoCompleteBox,它会正确抛出InvalidCastException,而不是NullReferenceException。
  • 是的,我有我的属性“DescField”来访问变量

标签: c# wpf mvvm


【解决方案1】:

here 所述,您可以使用 ItemTemplate(如果您需要,还有关于如何从代码创建 DataTemplate 的参考资料)。

here 来自 Jeff Wilcox 网站的 AutoCompleteBox 指南。

【讨论】:

  • 我尝试了这种方法,使用绑定创建了我的用户控件,但它保持不变
  • 现在列表返回给我的不是 DescField 而是客户端的全名 .Client 我想我缺少一些属性来仅显示该字段
  • 已经成功了!我必须将 ValueMemberPath="DescClient" 添加到控件:AutoCompleteBox。非常感谢
猜你喜欢
  • 2011-01-14
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2015-05-23
  • 2017-01-05
  • 1970-01-01
相关资源
最近更新 更多