[英]C#: Adding Null-Item of the same type of other Items to ComboBox
I have the problem that there are binding errors in my custom combobox (it derivates from the ComboBox from Windows Controls).我的问题是在我的自定义 combobox 中存在绑定错误(它派生自 Windows 控件的 ComboBox)。 There is a property called AddNullItem
which adds a ComboBoxItem
with the Content Empty
.有一个名为AddNullItem
的属性,它添加了一个ComboBoxItem
和 Content Empty
。
compositeCollection.Add(new ComboBoxItem() { Content = "Empty" });
The Problem is that if you bind to this ComboBox
and the Null-Item is selected, a binding error occurs, because the ComboBoxItems are not of the same type.问题是,如果您绑定到此ComboBox
并且选择了 Null-Item,则会发生绑定错误,因为 ComboBoxItems 的类型不同。
How can I add a ComboBoxItem of the same type of all other items or is there a better solution?如何添加与所有其他项目相同类型的 ComboBoxItem 或者是否有更好的解决方案?
My solution was following: compositeCollection.Add(Activator.CreateInstance(this.Items.GetType()));
我的解决方案如下: compositeCollection.Add(Activator.CreateInstance(this.Items.GetType()));
, but then occurs a XamlParseException. ,但随后发生 XamlParseException。
Okay, I got the solution:好的,我得到了解决方案:
I created the instance of the IEnumerable Items, not the single item.我创建了 IEnumerable Items 的实例,而不是单个项目。 I used following:我使用了以下内容:
object obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.