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

时间:2014-12-03 11:46:47

标签: c# 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");
    }
}

3 个答案:

答案 0 :(得分:14)

您可以使用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应该是视图模型属性。

答案 1 :(得分:1)

我遇到过类似的情况,我需要根据用户设置的值显示带有小数位数的双精度数。我用单例解决了。

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

MyConfiguration.Instance.NumberOfDecimals = user_defined_value;

答案 2 :(得分:0)

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