【问题标题】:WPF Combobox how to display value in LIST CollectionWPF Combobox如何在LIST集合中显示值
【发布时间】:2014-05-29 10:08:51
【问题描述】:

我有一个 List 集合,当它被分配时,它有一个短数字类型的集合。但是如何在 WPF ComboBox 上显示数字?

我通常在 XAML 中使用 DisplayMemberPath 属性,但我无法声明属性,因为它不是我自己的集合。 (如List(T)....)

XAML

<ComboBox HorizontalAlignment="Left" 
     ItemsSource="{Binding TheList}"
     // This is wrong of course
     DisplayMemberPath="." 

C#

public List<short> TheList
{
   get
   {
      return m_ListSID;
   }
   set
   {
      m_ListSID = value;
   }
 }

任何帮助将不胜感激。

编辑

愚蠢至极...所有答案当然都是正确且有道理的,只是因为我忘记触发集合的更新源

ItemsSource="{Binding TheList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

道歉

谢谢

【问题讨论】:

  • Short 是 C# 原始类型,我从未创建过它
  • 作为一种更好的编码习惯,集合应该是只读属性,并在备份属性为空时在属性的get部分初始化。

标签: c# wpf xaml combobox


【解决方案1】:

这里不需要DisplayMemberPath

<ComboBox HorizontalAlignment="Left"  ItemsSource="{Binding TheList}" DisplayMemberPath="" />

【讨论】:

    【解决方案2】:

    您不需要任何DisplayMemberPath
    这样就足够了:

    <ComboBox HorizontalAlignment="Left" ItemsSource="{Binding TheList}"/>
    

    【讨论】:

      【解决方案3】:

      根本不要设置DisplayMemberPathshort 应该转换为string

      <ComboBox HorizontalAlignment="Left" ItemsSource="{Binding TheList}"/>
      

      如果默认情况下没有提供模板,WPF 将在项目上调用ToString()

      编辑

      您可以在ContentPresenter MSDN 页面上找到有关如何创建显示内容的更多详细信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多