为什么我的类的行为与被重写的基类完全不同? [英] Why does my class not behave exactly like the overridden base class?
问题描述
我的应用程序有一个 ComboBox
,并且有一个类型的应用程序设置
。我将 myStrings
.Collections.Specialized.StringCollection ComboBox.ItemSource
绑定到 Properties.Settings.Default.myStrings
。 ComboBox按照我的计划显示了 myStrings
的项目。
不幸的是 ComboBox当
未更新项目。所以我尝试创建一个新类 myStrings
发生变化时, CustomStringCollection
,它会覆盖 StringCollection
并实现 INotifyPropertyChanged
。
设置未正确保存。我确实希望 CustomStringCollection:StringCollection
的行为与它的基类完全相同,包括保存时。为什么不一样?
这是设置文件,你可以看到 myStrings
-values被保存,但是 myCustomStringCollection
-values不是:
My application has a ComboBox
and had an application setting myStrings
of the type System.Collections.Specialized.StringCollection
. I bound the ComboBox.ItemSource
to Properties.Settings.Default.myStrings
. The ComboBox displayed the items of myStrings
, as I planned.
Unfortunately the ComboBox
did not update the items, when myStrings
changed. So I tried to create a new class CustomStringCollection
that overrides StringCollection
and implements INotifyPropertyChanged
.
The setting is not properly saved. I did expect that CustomStringCollection : StringCollection
does behave exactly like it's base class, including when it is saved. Why is it not the same?
This is the settings file and you can see that myStrings
-values are saved, but myCustomStringCollection
-values are not:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
…
<userSettings>
<comboBoxDataBinding.Properties.Settings>
<setting name="myStrings" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Banana</string>
</ArrayOfString>
</value>
</setting>
<setting name="myCustomStringCollection" serializeAs="Xml">
<value />
</setting>
</comboBoxDataBinding.Properties.Settings>
</userSettings>
</configuration>
这是类comboBoxDataBinding:
This is the class comboBoxDataBinding:
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
namespace comboBoxDataBinding {
[DefaultMember("Item")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
class CustomStringCollection : System.Collections.Specialized.StringCollection, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string v) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(v));
}
}
}
}
这就是我保存这些的方式设置:
And this is how I saved those settings:
Properties.Settings.Default.myStrings.Add("Banana");
Properties.Settings.Default.myCustomStringCollection.Add("Banana");
Properties.Settings.Default.Save();
推荐答案
首先,在您需要重置DataSource的位置ComboBox的属性...因为你的收藏已经改变了......试试这个:
First, at the point where you need to reset the DataSource Property of the ComboBox ... because your Collection has changed ... try this:
comboBox1.DataSource = null;
comboBox1.DataSource = customStringCollection;
comboBox1.ResetBindings();
其次,我建议你实现'INotifyCollectionChanged而不是'INotifyPropertyChanged:
Second, I suggest you implement 'INotifyCollectionChanged rather than 'INotifyPropertyChanged:
public class CustomStringCollection : StringCollection, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public new void Add(string item)
{
base.Add(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, this));
}
public new void Remove(string item)
{
base.Remove(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this));
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var handler = CollectionChanged;
if (handler != null) handler(this, e);
}
}
最后,我建议您使用自定义集合中的当前数据更新自定义'应用程序在FormClosing EventHandler中关闭时的设置。
Finally, I suggest you update the custom 'Setting when your Application closes in a FormClosing EventHandler using the current data in the custom collection.
这篇关于为什么我的类的行为与被重写的基类完全不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!