数据绑定列表框和 PropertyGrid 的更新问题
这是我的 AOI 类的一部分(没什么特别的):
class AOI : INotifyPropertyChanged
{
private Guid _id;
private string _name;
private string _comment;
public Guid Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string Comment
{
get { return _comment; }
set
{
_comment = value;
OnPropertyChanged("Comment");
}
}
public override string ToString()
{
if (_name.Length > 0)
{
return _name;
}
else
{
return _id.ToString();
}
}
}
我将它们保存在绑定到 ListBox
的 BindingList
中。在 ListBox
的 SelectedValueChanged
事件中,我将选定的对象分配给 PropertyGrid
,以便用户可以修改 AOI.
除了 Name 字段(显示在 ListBox
中(请参阅上面的 ToString()
))之外,此方法工作正常。
当我使用 PropertyGrid
编辑名称字段时,ListBox
会正确更新。但在 PropertyGrid
中,一旦我按下 Enter 键,Name
字段(只是值)就会被清除。当我将光标设置到 PropertyGrid
中的另一个字段时,会出现正确的(修改后的)值。
正确处理此问题的最简单的解决方法是什么?
This is part of my AOI class (nothing special about it):
class AOI : INotifyPropertyChanged
{
private Guid _id;
private string _name;
private string _comment;
public Guid Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string Comment
{
get { return _comment; }
set
{
_comment = value;
OnPropertyChanged("Comment");
}
}
public override string ToString()
{
if (_name.Length > 0)
{
return _name;
}
else
{
return _id.ToString();
}
}
}
I keep them in a BindingList<AOI>
which is bound to a ListBox
. In the SelectedValueChanged
event of the ListBox
I assign the selected object to a PropertyGrid
, so that the user can modify the AOI
.
This works fine except for the Name field (which is displayed in the ListBox
(see ToString()
above)).
When I edit the name field using the PropertyGrid
, the ListBox
is updated correctly. But in the PropertyGrid,
the Name
field (just the value) is cleared as soon as I press enter. The correct (modified) value appears when I set the cursor to another field in the PropertyGrid
.
What is the easiest workaround to handle this correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 PropertyGrid 的问题。
这也可以通过 Visual Studio 中的设计器进行重现。只需选择一个控件并将其最小尺寸更改为比当前尺寸更大的值即可。如果您查看网格,大小属性中的值将不会更新,直到您在网格中选择它。
如果某些行通常无法正确更新,则以下两个选项之一将有所帮助:
propertyGrid.SelectedObject = myObject
将对象重新附加到 PropertyGridpropertyGrid 强制网格重新绘制自身。无效()
This is a problem of the PropertyGrid.
This can also be reproduced with the designer within the Visual Studio. Simply select a control and change its minimum size to a larger value than the current one. If you take a look into the grid, the value in the size property won't be updated till you select it within the grid.
If some rows won't update correctly normally one of these two options will help:
propertyGrid.SelectedObject = myObject
propertyGrid.Invalidate()