2014-08-27 24 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(發行票)

相關問題