如何在 WPF 中将变量作为 Converterparameter 传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27270989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a variable as Converterparameter in WPF
提问by Vahid
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.
我正在尝试将在后面的代码中定义的变量作为ConverterParameter. 我将在转换器中使用此参数,然后决定某些单位转换。问题是我不知道如何通过这个。变量不是静态的。
<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");
}
}
回答by Dennis
You can use MultiBindingfor this purpose.
First, implement LengthConverteras IMultiValueConverter:
您可以MultiBinding为此目的使用。
首先,实现LengthConverter为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.Textwith multibinding:
二、TextBox.Text用多重绑定绑定:
<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.AncestorTypedepends on where CurrentDisplayUnitproperty is declared (the sample is for window's code behind).
注 1:RelativeSource.AncestorType取决于CurrentDisplayUnit声明属性的位置(示例用于窗口的代码后面)。
Note 2: looks like CurrentDisplayUnitshould be a view model property.
注 2:看起来CurrentDisplayUnit应该是一个视图模型属性。
回答by j.xavier.atero
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
我的转换器.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.
NumberOfDecimals 必须在调用 XALM 表单之前设置。
MyConfiguration.Instance.NumberOfDecimals = user_defined_value;
回答by RenDishen
ConverterParameter is not a dependency property and you cant bind any variable here.
ConverterParameter 不是依赖属性,您不能在此处绑定任何变量。

