我在 WPF/C# 中使用 null 和 ComboBox 时遇到一些困难;如果添加空项目,则不会在组合框中显示/列出。
这是我的 ComboBox 的 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);
绑定(bind)工作正常,如果我添加非空项目,则会列出这些项目。 我需要指定允许空值吗?
LG, 多米尼克
最佳答案
您可以尝试在绑定(bind)中使用 TargetNullValue 属性,并设置一些默认值。即,
{Binding Path=MyList, TargetNullValue="Empty Item"}
关于wpf - 带有绑定(bind)的 ComboBox 中未显示空项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634565/