简体   繁体   English

如何在WPF中将变量作为Converterparameter传递

[英]How to pass a variable as Converterparameter in WPF

I'm trying to pass a variable defined in the code behind as ConverterParameter . 我试图将后面代码中定义的变量作为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");
    }
}

You can use MultiBinding for this purpose. 您可以使用MultiBinding来实现此目的。
First, implement LengthConverter as IMultiValueConverter : 一是落实LengthConverterIMultiValueConverter

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: 其次,使用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.AncestorType depends on where CurrentDisplayUnit property is declared (the sample is for window's code behind). 注1: RelativeSource.AncestorType取决于声明CurrentDisplayUnit属性的位置(示例用于后面的窗口代码)。

Note 2: looks like CurrentDisplayUnit should be a view model property. 注2:看起来像CurrentDisplayUnit应该是一个视图模型属性。

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. 我有类似的情况,我需要根据用户设置的值显示一个带有小数位数的double。 I solved it using a Singleton. 我用Singleton解决了它。

MyConfiguration.cs 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 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. 必须在调用XALM表单之前设置NumberOfDecimals。

MyConfiguration.Instance.NumberOfDecimals = user_defined_value;

ConverterParameter不是依赖属性,你不能在这里绑定任何变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM