【问题标题】:Null items not shown in ComboBox with binding具有绑定的 ComboBox 中未显示空项
【发布时间】:2011-04-07 18:24:49
【问题描述】:

我在使用 WPF/C# 中的 null 和 ComboBox 时遇到了一些困难;如果添加一个 null 项,则它不会显示/列在 ComboBox 中。

这是我的 ComboBox 的 de XAML 声明

<ComboBox ItemsSource="{Binding Path=MyList}"
          SelectedItem="{Binding Path=Type}" />

MyList 被声明为

public ObservableCollection<Type> MyList { get; private set; }

并用

初始化
this.MyList = new ObservableCollection<Type>();
this.MyList.Add(null);

绑定工作正常,如果我添加非空项,则会列出。 是否需要指定允许空值?

lg, 多米尼克

【问题讨论】:

    标签: wpf binding combobox null observablecollection


    【解决方案1】:

    您可以尝试在绑定中使用 TargetNullValue 属性,设置一些默认值。即,

     {Binding Path=MyList, TargetNullValue="Empty Item"}
    

    【讨论】:

      【解决方案2】:

      ComboBox 就像我遇到的每个项目一样,无法为空项目呈现任何内容。我的理解是,如果没有要呈现的视觉内容,它会调用项目的 ToString() 方法并呈现它。由于您的项目为空,因此这是不可能的。

      我认为这个问答可能对你有所帮助。

      Display a default DataTemplate in a ContentControl when its content is null or empty?

      【讨论】:

        猜你喜欢
        • 2015-09-10
        • 1970-01-01
        • 2020-01-24
        • 2017-07-03
        • 2015-07-27
        • 1970-01-01
        • 2013-03-21
        • 2013-05-31
        • 2016-02-22
        相关资源
        最近更新 更多