How to pass variable as Converterparameter in WPF
I am trying to pass a variable defined in the code behind like ConverterParameter
. I will use this parameter in the converter to decide whether to change the unit. The problem is I don't know how to convey 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");
}
}
source to share
You can use MultiBinding
for this purpose.
First, let's implement it 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 the property is declared CurrentDisplayUnit
(sample for window code behind).
Note 2: it looks like the CurrentDisplayUnit
property of the view model should be.
source to share
I had a similar situation where I needed to show a double with decimal places based on a value set by the user. I solved it with 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 must be set before calling the XALM form.
MyConfiguration.Instance.NumberOfDecimals = user_defined_value;
source to share