星期三, 7月 07, 2021

[C#] 控件智能標籤 - 設定屬性值

[C#] 控件智能標籤 內有筆記該重點 - 更新要透過 PropertyDescriptor 來完成 
When a property or method in the class derived from DesignerActionList changes the state of the associated control, these changes should not be made by direct setter calls to the component's properties. Instead, such changes should be made through an appropriately created PropertyDescriptor. This indirect approach ensures that smart-tag undo and UI update actions function correctly.
從下圖可以看出,在智能標籤上把 Name 修改為 ChangeName 後,屬性視窗內的 Name 還是 flatButton1,沒有跟著變化
 
[C#] 控件智能標籤-設定屬性值

查 Code 才發現,原來使用方式是透過 Type.GetProperty().SetValue() 來設定屬性值,而不是透過 PropertyDescriptor
 
簡易紀錄
// 異常方式
Type.GetProperty().SetValue();

// 正確方式
TypeDescriptor.GetProperties().SetValue();

沒有留言:

張貼留言