2014-08-27 61 views
1

我试图编辑带有PropertyGrid的List<string>,并且它在修改内容时未触发PropertyValueChanged事件。编辑和触发PropertyGrid中的列表<string>的PropertyValueChanged事件

我研究了这一点,并尝试使用自定义TypeConverter类,但即使当我让编辑器显示并让我修改这些值时,我无法触发此事件。

我也尝试使用下面的属性,它拉起了字符串编辑器,但是这也不会触发事件的变化。

[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", 
    "System.Drawing.Design.UITypeEditor, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] 

我也尝试使用UITypeEditor并重写EditValue方法,但编辑值时,这永远不会触发。

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
{ 
    MessageBox.Show("This never appears..."); 
    return base.EditValue(context, provider, value); 
} 

是否有编辑List<string>火PropertyValueChanged事件的方法吗?

+0

你试试这个: [PropertyGrid中不会引发PropertyValueChanged事件] [1] [1]:http://stackoverflow.com/questions/15110594/propertygrid-does-not-raise-propertyvaluechanged-event – 2014-08-27 16:18:14

+0

是的,当通过PropertyGrid更改属性时,这不会调用事件。 – langstrom 2014-08-27 17:16:34

回答

1

您应该使用BindingList<string>而不是List<string>来获取PropertyValueChanged事件。

编辑:

@LarsTech指出,ObservableCollection<string>实际上是在WPF但没有使用的WinForms,你应该使用BindingList<string>代替。

总之,BindingList支持更多的接口和更多的功能比ObservableCollection。这里有一些优势去与BindingList

  • BindingList实现IBindingList<T>,但ObservableCollection没有。 IBindingList提供了一大堆可以被UI用来提供更多东西的功能,看here了解更多细节
  • BindingList implements ICancelAddNew数据绑定机制用于取消新添加的项目;
  • ObservableCollection不听其孩子的变化,但只收到InsertRemove事件;

点2和3个完整学分:ObservableCollection(Of T) vs BindingList(Of T)?

+0

在WinForms中,通常使用BindingList 。 – LarsTech 2014-08-27 18:22:02

+0

感谢您指出这一点,我编辑了该职位:) – nevets 2014-08-27 18:44:51

+2

这最终为我工作。我订阅了“BindingList”的ListChanged事件。此事件比列表中的项目数量多一次,所以我必须对此进行解释。不是我正在寻找的东西,但是这让我继续前进。谢谢!编辑:此外,这不会触发'PropertyValueChanged',至少不是我可以告诉。 – langstrom 2014-08-29 13:57:38

0

正如已经langstrom说明,没有的BindingList不会触发事件PropertyValueChanged。

我用一个简单的和丑陋的解决方法:我设置的完整集合(它只有几个项目),适应之后:

CollectionValue = CollectionValue

(我的目标是要解决一个红色边框如果IDataErrorInfo为已编辑的属性提供了一些错误,则为ObservableCollection(Of String)的定制PropertyGrid编辑器。)

另见

https://wpftoolkit.codeplex.com/discussions/544080(讨论)

https://wpftoolkit.codeplex.com/workitem/20977(发行票)