[英]How to pass a variable as Converterparameter in WPF
我試圖將后面代碼中定義的變量作為ConverterParameter
傳遞。 我將在轉換器中使用此參數然后決定某些單位轉換。 問題是我不知道如何通過這個。 變量不是靜態的。
<TextBox Text="{Binding MinimumRebarsVerticalDistance, Converter={StaticResource LengthConverter}, ConverterParameter={CurrentDisplayUnit}}"/>
代碼背后:
private Units currentDisplayUnit;
public Units CurrentDisplayUnit
{
get { return currentDisplayUnit; }
set
{
currentDisplayUnit = value;
RaisePropertyChanged("CurrentDisplayUnit");
}
}
您可以使用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)
{
// ...
}
}
其次,使用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>
注1: RelativeSource.AncestorType
取決於聲明CurrentDisplayUnit
屬性的位置(示例用於后面的窗口代碼)。
注2:看起來像CurrentDisplayUnit
應該是一個視圖模型屬性。
我有類似的情況,我需要根據用戶設置的值顯示一個帶有小數位數的double。 我用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();
}
}
必須在調用XALM表單之前設置NumberOfDecimals。
MyConfiguration.Instance.NumberOfDecimals = user_defined_value;
ConverterParameter不是依賴屬性,你不能在這里綁定任何變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.