Vahid
Vahid

Reputation: 5444

How to pass a variable as Converterparameter in 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");
    }
}

Upvotes: 8

Views: 22136

Answers (3)

j.xavier.atero
j.xavier.atero

Reputation: 584

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)
      {

         //--> Initialization
         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 XAML form.

MyConfiguration.Instance.NumberOfDecimals = user_defined_value;

Upvotes: 1

Dennis
Dennis

Reputation: 37780

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.

Upvotes: 19

RenDishen
RenDishen

Reputation: 938

ConverterParameter is not a dependency property and you cant bind any variable here.

Upvotes: 0

Related Questions