Bind to extension method in WPF

I have a simple class in C #:

public class Dog { public int Age {get; set;} public string Name {get; set;} } 

and I created an extension method as follows:

 public static class DogExt { public static string BarkYourName(this Dog dog) { return dog.Name + "!!!"; } } 

Is there a way to associate the result of the BarkYourName method with the wpf component?

Basically: is there a way for hwo to bind it to an extension method?

+4
source share
3 answers

No, you cannot bind to the extension method. You can bind to the Dog object's Name -property and use the converter though.

To create a converter, create a class that implements the IValueConverter interface. You only need a one-way conversion (from model to view), so you only need to implement the Convert method. The ConvertBack method ConvertBack not supported by your converter and thus throws a NotSupportedException .

 public class NameToBarkConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var dog = value as 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 you can use your converter as follows:

 <Grid> <Grid.Resources> <NameToBarkConverter x:Key="NameToBarkConverter" /> </Grid.Resources> <TextBlock Text="{Binding Name, Converter={StaticResource NameToBarkConverter}}" /> </Grid> 

For more information about converters, see MSDN: IValueConverter .

+6
source

Programming is an IQ logic game.

Binding to MethodName is not possible in WPF, since the binding mechanism only works with Properties. Although, you can use some tricks.

Here are some ways to do this.

  • Add an extension method to the accessory get properties, like this

     private string _name; public string Name { get { return _name.BarkYourName(); } set { _name = value; OnPropertyChanged("Name"); } } 
  • Add a converter to change the effect of the Name property, for example

     public class NameConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return "!!!"; return value.ToString().BarkYourName(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } public static class StringExt { public static string BarkYourName(this string str) { return str + "!!!"; } } 
+3
source

There is no way to bind to methods. You can create a property in the Dog class as shown below and bind it to it

 public string NameEx { get { return this.BarkYourName(); } } 

and if you want to synchronize Name and NameEx , just raise PropertyChanged -event for NameEx also in the Name -property installer.

+2
source

Source: https://habr.com/ru/post/1499339/


All Articles