我有一個屬性網格,其中一個屬性使用UITypeEditor編輯值(在窗體上)。PropertyGrid UITypeEditor禁用單元格編輯
但是,該屬性仍然是可編輯的,我不想要。有沒有辦法做到這一點?我看了這個類似的問題Propertygrid UIEditor disabling value editing through Keyboard,但它不能解決我的問題,因爲解決方案是使用TypeConverter的簡單下拉列表。
我有一個屬性網格,其中一個屬性使用UITypeEditor編輯值(在窗體上)。PropertyGrid UITypeEditor禁用單元格編輯
但是,該屬性仍然是可編輯的,我不想要。有沒有辦法做到這一點?我看了這個類似的問題Propertygrid UIEditor disabling value editing through Keyboard,但它不能解決我的問題,因爲解決方案是使用TypeConverter的簡單下拉列表。
一種解決方案是申報TypeConverter,做...什麼都沒有,這樣的事情:
這是你要編輯的類:
public class MyClass
{
[Editor(typeof(MyClassEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyConverter))]
public string MyProperty { get; set; }
}
這是自定義UITypeEditor的:
public class MyClassEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
MessageBox.Show("press ok to continue");
return "You can't edit this";
}
}
這就是著名的轉換器,把我天寫:
// this class does nothing on purpose
public class MyConverter : TypeConverter
{
}
我發現像這樣(PropertyValueChanged事件PropertyGrid的)一種解決方法:
private void propertyGridNewBonus_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
switch (e.ChangedItem.Label)
{
case "somePropertyLabel":
newBonus.somePropertyValue = e.OldValue.ToString();
break;
default:
break;
}
}
只是恢復舊值,而用戶在PropertyGrid中編輯它。 這看起來像值是可編輯的,但在合併舊值後恢復,因此改變它的唯一方法是使用自定義的TypeConverter,UITypeEditor等。
感謝您的回覆。這可能是我需要的。我會檢查這一點,如果我需要的話,我會將其標記爲正確的答案。 – dmcnally