Sunday, 15 March 2015

c# - Creating Dependency Properties for custom control -


After

I try to create a DependencyProperty for my custom control I am doing.

I want to be able to set a property, which will be set as a string, separated by commas, but as a list of strings Stored in memory

Here's what I have so far:

  public class FileTypeListControl: Control {public static read-only DependencyProperty FileTypeListProperty = DependencyProperty.Register ( "FileTypeList for", typeof (List the & lt; string & gt;), typeof (FileTypeListControl), new PropertyMetadata (new list of & lt; string & gt; () { ".txt"}, OnFileTypeListPropertyChanged, OnCoerceFileTypeListProperty), OnValidateFileTypeListPropety); Public listing & lt; String & gt; File Type List {Return (List & quot; String & gt;) GetValue (FileTypeListProperty); } Set {set value (file type listproperty, value); }} Public static void OnFileTypeListPropertyChanged (DependencyObject source, DependencyPropertyChangedEventArgs e) {} public static object OnCoerceFileTypeListProperty (DependencyObject from object data) {if {var val = (data as a string) KSplit (new char [] (data string } {','}); Return valve; } And if (list of data & lt; string & gt;) {return data; } Other {new list & lt; String & gt; (); }} Public Stable Bull OnValidateFileTypeListPropety (Object Data) {Return Truth; }} & Lt; Local: FileTypeListControl X: ".xml, java, .exe" name = "control" Grid.Column = "2" fileTypeList = /> Now, the problem is that the way I specified the  FileTypeList  property in xaml is not liked, I was under the impression that  OnCoerceFileListProperty  method was to emphasize value in the appropriate type, which I am doing 

I can use a value converter, but every time I re-use control I do not think so right.

What am I doing wrong here?

Type conversions by this Cosavl value callback does not work usually TypeConverters objective is to Public class StringList Commander: TypeConverter {Public Override Bull CanConvertFrom (ITypeDescriptorContext Reference, Type Source Type) {Source Source Type == Type (string); } Public override object Knhwartfom (Aitiipi descriptor contact reference, Kalcrinfo culture, Object value) {return (string) value) Ksplit ( ''); }} ... [correspondent (typef (stringlist command)) public ILIST & lt; String & gt; File Type List {Return (ILIIT & lt; string & gt;) GetValue (File Type Listproperty); } Set {set value (file type listproperty, value); <}>

Also keep in mind that property must be of the IList & lt; String & gt; By declaring it as list & lt; String & gt; .

You will be given the new list as your default value for the property & lt; String & gt; () { ".txt"} as to avoid the use of any expression, because this value is used for all FileTypeList the "examples" of your estate when you upset a control frequency < / Code> Add a string for the property, this change will also occur in other control frequency, as long as they have a default property value.

The default value should be null for all reference type properties, then you can set the default default value in the control creator:

  public FileTypeListControl () {SetCurrentValue (FileTypeListProperty, new list & lt; string & gt; () {".txt"}); }  

No comments:

Post a Comment