绑定到 Specialized.StringDictionary 的列表框不会更新
我有一个列表框(listStorageLocations) - 它绑定到名为 listStorageLocationsBindingSource 的 BindingSource。此 BindingSource 绑定到我的 Settings.Default.StorageLocations 中的 StringDictionary 项的集合。然后将列表框的数据源设置为绑定源。我一生都无法在这个该死的列表框中显示任何内容(除非我专门添加测试项目)。
下面是一些代码:
表单初始化:
listStorageLocations.DisplayMember = "Key";
listStorageLocations.ValueMember = "Value";
listStorageLocationsBindingSource.DataSource = Settings.Default.StorageLocations;
listStorageLocations.DataSource = listStorageLocationsBindingSource;
if (Settings.Default.StorageLocations == null)
Settings.Default.StorageLocations = new StringDictionary();
按钮逻辑,添加新的存储位置:
private void btnAddStorageLocation_Click(object sender, EventArgs e)
{
if (!txtStorageLocation.Text.Equals("") && !(txtStorageLocationName.Text.Equals("") &&
!(StorageLocationsContainsPath(txtStorageLocation.Text)) &&
!(StorageLocationsContainsName(txtStorageLocationName.Text))))
{
Settings.Default.StorageLocations.Add(txtStorageLocationName.Text, txtStorageLocation.Text);
}
Settings.Default.Save();
listStorageLocationsBindingSource.ResetBindings(false);
}
我尝试过将手臂举起 78 度角、跳过 7 个燃烧的铁环、倒立,但都无济于事。我错过了他们的一些愚蠢的细节吗?
哦,我在这里调用 Settings.Default.Save() 只是为了在我的 StringDictionary 中获取一些初始值 - 但当然 - 这也不起作用,因为当我重新启动应用程序时 - 没有项目。
I've got a listbox (listStorageLocations) - thats bound to a BindingSource called listStorageLocationsBindingSource. This BindingSource is bound to a collection of StringDictionary items in my Settings.Default.StorageLocations. The listbox's datasource is then set to the bindingsource. I cannot for the life of me get anything to display in this blasted listbox (unless I specifically add test items).
Here's some code:
Form Initialization:
listStorageLocations.DisplayMember = "Key";
listStorageLocations.ValueMember = "Value";
listStorageLocationsBindingSource.DataSource = Settings.Default.StorageLocations;
listStorageLocations.DataSource = listStorageLocationsBindingSource;
if (Settings.Default.StorageLocations == null)
Settings.Default.StorageLocations = new StringDictionary();
Button logic, to add a new storage location:
private void btnAddStorageLocation_Click(object sender, EventArgs e)
{
if (!txtStorageLocation.Text.Equals("") && !(txtStorageLocationName.Text.Equals("") &&
!(StorageLocationsContainsPath(txtStorageLocation.Text)) &&
!(StorageLocationsContainsName(txtStorageLocationName.Text))))
{
Settings.Default.StorageLocations.Add(txtStorageLocationName.Text, txtStorageLocation.Text);
}
Settings.Default.Save();
listStorageLocationsBindingSource.ResetBindings(false);
}
I've tried holding my arm up at a 78 degree angle, jumping through 7 flaming hoops, and standing on my head to no avail. Is their some stupid detail I am missing?
Oh also I'm calling Settings.Default.Save() right here just to get some intial values in my StringDictionary - but of course - that doesn't work either because when I restart app - there are no items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我发现, StringDictionary 类不是可序列化的,所以我的设置没有用 Settings.Default.Save() 保存 - 所以我的方向是错误的。以不同的方式存储数据。
Well I figured it out, the StringDictionary class isnt Serializable so my settings aren't saved with Settings.Default.Save() - so I'm headed in the wrong direction with this. Going to store data differently.