PropertyGrid中没有注意到的代码更改的属性? [英] PropertyGrid doesn't notice properties changed in code?

查看:138
本文介绍了PropertyGrid中没有注意到的代码更改的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用颜色来突出某些事物的WinForm应用程序。我想允许用户更改他们的色彩。作为练习,我以为我会创建一个类的实例,与颜色的属性,并将其分配给一个属性网格的(得到一个不错的编辑器)



这似乎做工精细,但转念一想,我想,让用户重新设置的的颜色(在他们摆弄,并将其设置为20色调的米色)的。所以,我增加了一个重置按钮,我的形式,设置我的对象回默认的颜色属性。



但是,似乎,虽然它集我对象的属性后面,属性网格不会改变。如果在复位后,我做了属性网格刷新,它具有复位颜色。



我假设属性网格不知道底层对象已更改?



有什么在这种情况下丢失,或者我应该接受它,并调用Refresh方法时重置我的对象吗?



感谢



(非常类似的问题的这里

 公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();

this.propertyGrid1.SelectedObject =新的色彩();
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
色彩色= this.propertyGrid1.SelectedObject的颜色;
colours.Reset();
}
}

公共部分类颜色:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;

众彩ColourP1 {搞定;组; }

公共无效复位()
{
this.ColourP1 = Color.Red;
VAR处理器= this.PropertyChanged;
如果(处理!= NULL)处理(这一点,新PropertyChangedEventArgs(ColourP1));
}
}






从我的评论继的没什么订阅了INotifyPropertyChanged.PropertyChanged的,什么是在subsclassing PropertyGrid控件,以便它呢?



<$缺点p $ p> 公共部分类MyPropertyGrid:System.Windows.Forms.PropertyGrid
{
私人INotifyPropertyChanged的_selectedObject;

保护覆盖无效OnSelectedObjectsChanged(EventArgs的发送)
{
base.OnSelectedObjectsChanged(E);

如果(_selectedObject!= NULL)_selectedObject.PropertyChanged - = selectedObject_PropertyChanged;
_selectedObject = this.SelectedObject为INotifyPropertyChanged的;
如果(_selectedObject!= NULL)_selectedObject.PropertyChanged + = selectedObject_PropertyChanged;
}

私人无效selectedObject_PropertyChanged(对象发件人,PropertyChangedEventArgs E)
{
this.Refresh();
}
}


解决方案

要回答你为什么PropertyGrid中不会改变的问题,MSDN文档的 PropertyGrid的说这




在网格中显示的信息是在对象分配时的属性
的快照。如果由SelectedObject指定的对象$ B $为b的属性值在代码在运行时改变,不显示
新值,直到采取行动在于
中的网格使网格刷新。




所以,似乎PropertyGrid中是不是是自动更新的控件。我认为,解决问题的线索,这是该PropertyGrid中使用 SelectedObject 方法,而不是一个数据源方法,而后者会暗示它可能听INotifyPropertyChanged的。



你留下什么LarsTech曾建议和手动刷新网格。


I have a Winform application which uses colour to highlight certain things. I would like to allow the users to change 'their' colours. As an exercise, I thought I would create an instance of a class, with properties for the colours, and assign it to a property grid (to get a nice editor).

This seems to work fine, but I then thought I would like to let the users reset the colours (after they had fiddled and set them to 20 shades of beige). So, I added a "reset" button to my form, which set the colour properties of my object back to the defaults.

However, it seems that while it sets my object's properties back, the property grid doesn't change. If, after the reset, I do a property grid "Refresh", it has the reset colour.

I'm assuming that the property grid doesn't know that the underlying object has been changed ?

Is there something missing in this scenario, or should I just accept it and call the Refresh method when I reset my object ?

Thanks

(very similar question here)

public partial class Form1 : Form
{
  public Form1()
  {
     InitializeComponent();

     this.propertyGrid1.SelectedObject = new Colours();
  }

  private void button1_Click(object sender, EventArgs e)
  {
     Colours colours = this.propertyGrid1.SelectedObject as Colours;
     colours.Reset();
  }
}

public partial class Colours : INotifyPropertyChanged 
{
  public event PropertyChangedEventHandler PropertyChanged;

  public Color ColourP1 { get; set; }

  public void Reset()
  {
     this.ColourP1 = Color.Red;
     var handler = this.PropertyChanged;
     if (handler != null) handler(this, new PropertyChangedEventArgs("ColourP1"));
  }
}


Following on from my comment of "nothing subscribes to the INotifyPropertyChanged.PropertyChanged", what's the drawback in subsclassing the PropertyGrid control so that it does ?

public partial class MyPropertyGrid : System.Windows.Forms.PropertyGrid
{
  private INotifyPropertyChanged _selectedObject;

  protected override void OnSelectedObjectsChanged(EventArgs e)
  {
     base.OnSelectedObjectsChanged(e);

     if (_selectedObject != null) _selectedObject.PropertyChanged -= selectedObject_PropertyChanged;
     _selectedObject = this.SelectedObject as INotifyPropertyChanged;
     if (_selectedObject != null) _selectedObject.PropertyChanged += selectedObject_PropertyChanged;
  }

  private void selectedObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
     this.Refresh();
  }
}

解决方案

To answer your question on why the PropertyGrid doesn't change, the MSDN documentation for the PropertyGrid say this:

The information displayed in the grid is a snapshot of the properties at the time the object is assigned. If a property value of the object specified by the SelectedObject is changed in code at run time, the new value is not displayed until an action is taken in the grid that causes the grid to refresh.

So, it seems that the PropertyGrid is not a control that is auto-updatable. I think the clue to this is that the PropertyGrid uses the SelectedObject method instead of a DataSource method, and the latter would imply it probably listens to INotifyPropertyChanged.

You're left with what LarsTech has suggested and manually refreshing the grid.

这篇关于PropertyGrid中没有注意到的代码更改的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆