将绑定控件更新到 ApplicationSettings
我第一次尝试使用 .NET 2.0 ApplicationSettings 功能,并发现它在某些方面有点……令人费解。 我希望有人能帮助我找出我哪里出了问题。
我有一个通用设置类,我已经实现了它,它是 ApplicationSettingsBase 的子类。 我已经实现了一个属性,并对其进行了标记。 这似乎有效,
然后我尝试将列表框控件绑定到该属性,这似乎也有效。 当我打开表单时,它会很好地加载属性。
我遇到的问题是,如果我想更新设置和数据绑定而不重新加载表单,它似乎不起作用。 没有方法可以刷新绑定,从我读到的内容来看,它应该只是自动更新(ApplicationSettingsBase 实现 IPropertyChangeNotify,DataBindings 应该订阅它..但它似乎不起作用)。 如果您有数据绑定,您也无法手动更新列表框。
所以这是我的代码:
Settings.cs
public class Settings : ApplicationSettingsBase
{
public Settings()
{
if (FixedBooks == null) FixedBooks = new List<string>();
}
[UserScopedSetting()]
public List<string> FixedBooks
{
get { return (List<string>)this["FixedBooks"]; }
protected set { this["FixedBooks"] = value; }
}
}
SettingsForm.cs
Settings _settings = new Settings();
private void SettingsForm_Load(object sender, EventArgs e)
{
lbFixedColumns.DataBindings.Add(new Binding("DataSource", _settings,
"FixedBooks", false, DataSourceUpdateMode.OnPropertyChanged));
}
private void DoSomething()
{
_settings.FixedBooks.Add("Test");
}
我的理解是,向 ApplicationSettings 添加某些内容应该触发 IPropertyChangedNotify 以提醒控件绑定属性已更改,并强制其重新加载..但这似乎不是正在发生。
我缺少什么?
编辑:
我相信我知道问题是什么。 问题是我正在更改 Settings 类中集合的内容,但没有更改实际属性本身(即集合本身)。 我认为我必须实际添加或删除整个集合才能触发 IPropertyChangedNotify,但这并没有发生。
我不确定这个问题的解决方案是什么。
I'm attempting to utilize the .NET 2.0 ApplicationSettings feature for the first time, and find it a bit... puzzling in some ways. I'm hoping someone can help me figure out where i'm going wrong.
I have a generic settings class which i've implemented that is a subclass of ApplicationSettingsBase. I've implemented a property, and tagged it. This seems to work
I've then tried to bind a listbox control to the property, and this also seems to work. When I open the form, it loads the properties just fine.
The problem i'm running into is that if I want to update the settings and databinding without reloading the form, it doesn't seem to work. There are no methods to refresh the binding, and from what i'm reading, it should just auto-update (ApplicationSettingsBase implements IPropertyChangeNotify, which DataBindings should subscribe to.. but it doesn't seem to be working). You also can't manually update the listbox if you have a DataBinding.
So here's my code:
Settings.cs
public class Settings : ApplicationSettingsBase
{
public Settings()
{
if (FixedBooks == null) FixedBooks = new List<string>();
}
[UserScopedSetting()]
public List<string> FixedBooks
{
get { return (List<string>)this["FixedBooks"]; }
protected set { this["FixedBooks"] = value; }
}
}
SettingsForm.cs
Settings _settings = new Settings();
private void SettingsForm_Load(object sender, EventArgs e)
{
lbFixedColumns.DataBindings.Add(new Binding("DataSource", _settings,
"FixedBooks", false, DataSourceUpdateMode.OnPropertyChanged));
}
private void DoSomething()
{
_settings.FixedBooks.Add("Test");
}
My understanding is that adding something to the ApplicationSettings should fire the IPropertyChangedNotify to alert the control binding that the property has changed, and force it to reload.. but this doesn't seem to be happening.
What am I missing?
EDIT:
I believe I know what the problem is. The problem is that i'm altering the contentes of the collection in the Settings class, but not changing the actual property itself (which is the collection itself). I think i would have to actually add or remove the entire collection to get the IPropertyChangedNotify to fire, which isn't happening.
I'm not sure what the solution to this problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这不是一个很好的解决方案,我希望有人有更好的解决方案,但这很有效:
我尝试简单地将自身重新分配给自己,但显然有一些优化可以检测到这一点并且无法触发 PropertyChanged。
我非常乐意为提出更好解决方案的人提供答案。
编辑:
好的,我想我已经找到了一个可以接受的解决方案,它只是有点混乱,并且不需要无缘无故地创建像上面这样的新对象。
好的,首先,我向 Settings 类添加一个新方法:
这允许我触发 PropertyChanged 事件,而无需实际更改属性。 然而,Binding 类对我来说仍然有点太聪明了,因为它知道属性实际上没有改变......所以它什么也不做。
因此,这段代码伪造了 Binding 类,通过清除 DataSource 现在它无法判断该属性尚未更改,因此它必须提取最新版本的数据。 我可以忍受这个,尽管它还是有点臭。
EDIT2:
正如 drs 指出的那样,我应该使用 BindingList 而不是 List,使用 BindingList 是正确的解决方案。
Ok, not a great solution, and I'm hoping someone has a better one, but this works:
I tried simply reassigning itself to itself, but there is apparently some optimziation in there that detects this and fails to fire the PropertyChanged.
I'd be more than happy to give an answer to someone that comes up with a better solution.
EDIT:
Ok, I think i've found an acceptable solution that's only a little kludgey, and doesn't require the creation of new objects like the above for no reason.
Ok, first, I add a new method to my Settings class:
This allows me to trigger a PropertyChanged event without actually changing the property. However, the Binding class is still a little too smart for me, because it knows that the property hasn't actually changed.. so it does nothing.
So, this code fakes out the Binding class, by clearing the DataSource it now can't tell that the property hasn't changed, so it has to pull the latest version of the data. I can live with this, even though it's still a little stinky.
EDIT2:
As drs points out, I should have been using a BindingList rather than a List, using a BindingList is the correct solution.
您需要保存设置并重新加载它:
我同意您的编辑和评论。 尝试使用 BindingList 代替。
例子:
You need to save the setting and reload it:
I agree with your edit and your comment. Try using a BindingList instead.
Example: