Use binding as ConverterParameter

I am trying to use value binding as a converter parameter, as shown in the code snippet below:

<Element Attribute="{Binding Value, Converter={StaticResource EqualityConverter}, ConverterParameter={Binding CompareTo}}" /> 

The problem is that the EqualityConverter::Convert() method is called with the Binding instance as the converter parameter ( CompareTo ), and not the specific binding value.

Is there a smarter way to solve this problem? I could explicitly provide the converted property in my view model, but I would like to know if there is a similar working solution for the above.

+7
c # xaml xamarin xamarin.forms
source share
3 answers

ConverterParameter is not a dependency property, so you cannot bind any property. However, you can try using MultiBinding with MultiValueConverter.

the code:

 <TextBox> <TextBox.Text> <MultiBinding Converter="{StaticResource MultiValueConverter}"> <Binding Path="property1"/> <Binding Path="property2"/> </MultiBinding> </TextBox.Text> </TextBox> 
 public class MultiValueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { property1 = values[0]; property2 = values[1]; // Do your logic with the properties here. } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { } } 
+2
source share

Another, potentially easier way to do this is to define the bindable property in the converter itself.

 public class CompareConverter: IValueConverter, INotifyPropertyChanged{ private ComparisonType _comparison = ComparisonType.Equals; public ComparisonType Comparison { get {return _comparison; } set { _comparison = value; OnPropertyChanged(); } } private string _compareTo = null; public string CompareTo { get {return _compareTo; } set { _compareTo = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool result = false; switch (Comparison)... return result; } ... } 

You can also do this by inheriting from BindableObject and implementing the bindable properties, you may need to do this if the binding context is not transferred to resources. If this is the case, you can install it from the code at a time in the constructor, after calling the Initialize method.

Your haml will look like this:

 .. <ResourceDictionary> <myPrefix:CompareConverter x:Key="compareToY" Comparison="Equals" CompareTo="{Binding... }"/> </ResourceDictionary> ... <Control Value="{Binding X, Converter={StaticResource compareToY}}"/> 

A little adjustment may be required, the result should be cleaner than multibinding

+8
source share

I struggled with the same problem and did not get the data I needed. You cannot be attached to the "converter parameter", that is, how it is done on the current date. But I really wanted to get some data sent to the parameter. so I found a simple but working solution, and I hope that it can work for you too. Start by providing CompareTo x: Name = "CompareTo" or whatever you want to name.

  <Element Attribute="{Binding Value, Converter={StaticResource EqualityConverter}, ConverterParameter={x:reference CompareTo}}" /> 

By doing x: referring to its actually sending some data, you just have to capture it. For me, the value I needed was the value "string" so that I could follow certain "If" instructions. This way you can do something similar to:

  if(CompareTo == "myCoolString") { Value = "Well i guess i'm not so cool!" } 

This is how I got the data from the parameter:

  public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter != null) { try { var image = parameter as Image; var src = image.Source as FileImageSource; if (src.File.ToString() == "coolimage.png") { return "thumbsup.png"; } } catch { } } } 

In my case, I was working with an image and needed to find out if there was one image “A” and then “B” in order to change the image “C”. This should work with other objects. A little lucky, this brings you closer to some simplified "Multibinding" and answer.

Hope this has been helpful since this is my first post on Stackoverflow!

+3
source share

All Articles