c# - Bind to extension method in WPF -


i have simple class in c#:

public class dog {     public int age {get; set;}     public string name {get; set;} } 

and created extension method :

public static class dogext {     public static string barkyourname(this dog dog) {         return dog.name + "!!!";     } } 

is there way how bind result of barkyourname method wpf component?

basically : there way hwo bind extension method?

no, cannot bind extension method. can bind name-property of dog-object , use converter though.

to create converter create class implementing ivalueconverter interface. need one-way conversion (from model view) need implement convert method only. convertback method not supported converter , throws notsupportedexception.

public class nametobarkconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         var dog = value dog;         if (dog != null)         {             return dog.barkyourname();         }         return value.tostring();     }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notsupportedexception();     } } 

then can use converter follows:

<grid>     <grid.resources>         <nametobarkconverter x:key="nametobarkconverter" />     </grid.resources>     <textblock text="{binding name, converter={staticresource nametobarkconverter}}" /> </grid> 

for more information on converters see msdn: ivalueconverter.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -