Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable as Converterparameter in WPF

Tags:

c#

wpf

I'm trying to pass a variable defined in the code behind as ConverterParameter. I'll use this parameter in the converter then to decide on some unit conversion. The problem is I don't know how to pass this. The variable is not static.

<TextBox Text="{Binding MinimumRebarsVerticalDistance, Converter={StaticResource LengthConverter}, ConverterParameter={CurrentDisplayUnit}}"/>

Code behind:

private Units currentDisplayUnit;
public Units CurrentDisplayUnit
{
    get { return currentDisplayUnit; }
    set
    {
        currentDisplayUnit = value;
        RaisePropertyChanged("CurrentDisplayUnit");
    }
}
like image 222
Vahid Avatar asked Dec 03 '14 11:12

Vahid


2 Answers

You can use MultiBinding for this purpose.
First, implement LengthConverter as IMultiValueConverter:

public sealed class LengthConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // values array will contain both MinimumRebarsVerticalDistance and 
        // CurrentDisplayUnit values
        // ...
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        // ...
    }
}

Second, bind TextBox.Text with multibinding:

        <TextBox.Text>
            <MultiBinding Converter="{StaticResource LengthConverter}">
                <Binding Path="MinimumRebarsVerticalDistance"/>
                <Binding Path="CurrentDisplayUnit" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}"/>
            </MultiBinding>
        </TextBox.Text>

Note 1: RelativeSource.AncestorType depends on where CurrentDisplayUnit property is declared (the sample is for window's code behind).

Note 2: looks like CurrentDisplayUnit should be a view model property.

like image 82
Dennis Avatar answered Oct 15 '22 13:10

Dennis


I had a similar situation where I needed to show a double with a number of decimals based on a value set by the user. I solved it using a Singleton.

MyConfiguration.cs

   public sealed class MyConfiguration
   {
      #region Singleton
      private static readonly Lazy<MyConfiguration> lazy = new Lazy<MyConfiguration>(() => new MyConfiguration());
      public static MyConfiguration Instance { get { return lazy.Value; } }
      private MyConfiguration() {}
      #endregion

      public int NumberOfDecimals { get; set; }
   }

MyConverters.cs

   /// <summary>
   /// Formats a double for display in list
   /// </summary>
   public class DoubleConverter : IValueConverter
   {
      public object Convert(object o, Type type, object parameter, CultureInfo culture)
      {

         //--> Initializations
         IConvertible iconvertible__my_number = o as IConvertible;
         IConvertible iconvertible__number_of_decimals = parameter as IConvertible;

         //--> Read the value
         Double double__my_number = iconvertible__my_number.ToDouble(null);

         //--> Read the number of decimals       
         int number_of_decimals = MyConfiguration.Instance.NumberOfDecimals; // get configuration
         if (parameter != null)  // the value can be overwritten by specifying a Converter Parameter
         {
            number_of_decimals = iconvertible__number_of_decimals.ToInt32(null);
         }

         //--> Apply conversion
         string string__number = (Double.IsNaN(double__number)) ? "" : (number_of_decimals>=0) ? Math.Round(double__my_number, number_of_decimals).ToString(): double__my_number.ToString();

         return string__number;
      }

      public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
      {
         throw new NotSupportedException();
      }
   }

NumberOfDecimals has to be set before calling the XALM form.

MyConfiguration.Instance.NumberOfDecimals = user_defined_value;
like image 31
j.xavier.atero Avatar answered Oct 15 '22 11:10

j.xavier.atero