Propertygrid UIEditor disabling value editing through Keyboard

谁说胖子不能爱 提交于 2019-12-11 02:04:38

问题


I have a propertygrid that uses UITypeEditor to display a Listbox and select a item. This item gets returned on the proertygrid on selection. But how do I disable editing of the selected item directly, readonly option prevents it from getting edited.

Let's say I'm changing a Property Country from another form which displays a list of countries in a listbox. When I select 'Algeria' and press OK, 'Algeria' is displayed across Country, but I can directly edit Algeria to any other value, defeating the purpose of a List to choose from.


回答1:


If you just want to show a drop-down list of values to select from, but prevent the user from typing anything into the property value within the PropertyGrid control, you can derive from TypeConverter and override the GetStandardValuesExclusive method to simply return true.

To provide the set of allowable values to show in the drop-down you need to override the GetStandardValuesSupported method to return true then override GetStandardValues to return the list of allowable values you want to show in the drop-down list.

Once you have that in place, you just need to specify your custom type converter on the property like so:

public class MyTypeConverter : TypeConverter
{
  //Override GetStandardValuesExclusive, 
  //GetStandardValues and GetStandardValuesSupported
}

public class SomeClass
{

   [TypeConverter(typeof(MyTypeConverter))]
   public string SomePropertry
   {
      ...
   }
}


来源:https://stackoverflow.com/questions/7021211/propertygrid-uieditor-disabling-value-editing-through-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!