RefreshPropertiesAttribute不刷新PropertyGrid [英] RefreshPropertiesAttribute does not refresh PropertyGrid
问题描述
RefreshPropertiesAttribute
不能强制刷新PropertyGrid
是否存在已知问题?
我有一个包含PropertyGrid
的简单表单.我创建一个对象,并将其提供给PropertyGrid.SelectedObject
.我看到显示了该对象的属性.
然后,我单击一个导致该对象的属性之一的值更改的测试按钮.但是,PropertyGrid
不会刷新以显示新值.仅当我单击该属性的网格单元时,它才会刷新.
我用RefreshPropertiesAttribute
装饰了该属性,如下所示,但没有效果.
Is there a known problem with the RefreshPropertiesAttribute
not being able to force PropertyGrid
to refresh?
I have a simple form that contains a PropertyGrid
. I create an object, and give it to PropertyGrid.SelectedObject
. I see the properties of that object displayed.
I then click a test button that causes the value of one of the object''s properties to change. However, the PropertyGrid
does not refresh to display the new value. Only when I click on the grid cell for that property does it refresh.
I decorated the property with the RefreshPropertiesAttribute
as follows, but it has no effect.
public class Foo
{
private bool m_Flag;
[RefreshProperties( RefreshProperties.All )]
public bool Flag
{
get { return m_Flag; }
set { m_Flag = value; }
}
}
我创建了自己的从PropertyGrid
派生的CustomPropertyGrid
类,并覆盖了Refresh()
.因此,我确定不会调用Refresh()
.
I created my own CustomPropertyGrid
class that derives from PropertyGrid
and overrode Refresh()
. Thus, I am certain that Refresh()
is not being called.
public class CustomPropertyGrid : System.Windows.Forms.PropertyGrid
{
public override void Refresh( )
{
base.Refresh();
Debug.WriteLine( "*** Refresh ***" );
}
}
我的开发环境是:
-Visual Studio 2008
-目标框架= .NET 2.0
感谢所有建议.
My development environment is:
- Visual Studio 2008
- Target Framework = .NET 2.0
Any suggestions are appreciated.
推荐答案
我现在意识到,RefreshPropertiesAttribute
仅在PropertyGrid
更改对象中的属性值时才起作用.
解决我的问题的方法是:
1.让我的对象实现一个接口,当其属性之一更改时,该接口引发一个特殊事件.例如,INotifyPropertyChanged
是DotNet框架提供的标准版本.
2.创建派生的PropertyGrid
,该派生的PropertyGrid
处理分配给PropertyGrid.SelectedObjects
的对象中的特殊事件.
3.在该处理程序中,调用PropertyGrid.Refresh()
.
I realize now that theRefreshPropertiesAttribute
only has an effect when thePropertyGrid
changes the value of a property in my object.
The solution to my problem is to:
1. Have my object implement an interface that raises a special event when one of its properties changes. For example,INotifyPropertyChanged
is a standard one provided by the DotNet framework.
2. Create a derivedPropertyGrid
that handles that special event from the objects assigned toPropertyGrid.SelectedObjects
.
3. In that handler, callPropertyGrid.Refresh()
.
这篇关于RefreshPropertiesAttribute不刷新PropertyGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!