将多个绑定值作为绑定CONVERTER PARAMETER传递

时间:2016-12-16 07:05:13

标签: c# wpf binding converter

我需要将两个值(在可能的情况下称为Unit和Quantity)作为转换器参数传递给绑定转换器。(注意,我不需要将这些值作为绑定值传递(multibinding),我需要传递它们作为绑定转换器参数因为,我需要转换器的ConvertConvertBack方法。 我认为唯一可行的方法是创建一个新的类 UnitQuantityBindClass 来在该类中设置它们并将此类作为转换器参数传递,但此类不会获取绑定值,当我通过转换器,转换器参数,即创建的类,没有值。 一个人可以帮我吗?

public class UnitQuantityBindClass:DependencyObject
{
    public static readonly DependencyProperty QuantityProperty = DependencyProperty.Register(
        "Quantity", typeof(EQuantities), typeof(UnitQuantityBindClass));

    public EQuantities Quantity
    {
        get { return (EQuantities)GetValue(QuantityProperty); }
        set { SetValue(QuantityProperty, value); }
    }

    public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
        "Unit", typeof(Enum), typeof(UnitQuantityBindClass));

    public Enum Unit
    {
        get { return (Enum)GetValue(UnitProperty); }
        set { SetValue(UnitProperty, value); }
    }
}

用法:

<textboxunitconvertor:TextBoxUnitConvertor Name="gasDensityValueControl" InstantaneousConvert="True" Margin="96,163,0,0" IsEnabled="{Binding ElementName=chkGas,Path=IsChecked}" QuantityBind="{Binding _FluidBlackOilClass.SGGas_SC.Quantity , RelativeSource={RelativeSource AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  Width="206" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
    <textboxunitconvertor:TextBoxUnitConvertor.TextBoxText>
        <Binding Path="_FluidBlackOilClass.SGGas_SC.Value" RelativeSource="{RelativeSource AncestorType=Window}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" Converter="{StaticResource ValueStorageForUnitConverter}">
             <Binding.ConverterParameter>
                 <classes:UnitQuantityBindClass
                     Quantity="{Binding ElementName=gasDensityValueControl,
                                Converter={StaticResource DummyConverter},
                                Path=_Quantity,
                                UpdateSourceTrigger=PropertyChanged,
                                Mode=TwoWay,
                                PresentationTraceSources.TraceLevel=High}"
                     Unit="{Binding ElementName=gasDensityValueControl,
                            Path=_CurrentUnitEnum,
                            UpdateSourceTrigger=PropertyChanged,
                            Mode=TwoWay}" />
             </Binding.ConverterParameter>
        </Binding>
    </textboxunitconvertor:TextBoxUnitConvertor.TextBoxText>
</textboxunitconvertor:TextBoxUnitConvertor>   

注意:我的要求是获取“_FluidBlackOilClass.SGGas_SC.Value”并将其传递给转换器,我还需要传递“_Quantity”和“_CurrentUnitEnum”作为转换器参数来转换“_FluidBlackOilClass.SGGas_SC.Value”根据“_Quantity”和“_ CurrentUnitEnum”将新值设置为TextBoxText。我还需要根据“_Quantity”和“_CurrentUnitEnum”转换TextBoxText以存储在“_FluidBlackOilClass.SGGas_SC.Value”中。

1 个答案:

答案 0 :(得分:1)

  1. 让您的Converter继承Freezable,并引入一个名为SourceTextBox的DP,它将引用您的TextBox,然后在您的Convert和{{ 1}}方法,您可以使用此引用来获取所需的属性。

    ConvertBack
  2. 用法:

    public class BindableConverter : Freezable, IValueConverter
    {
        #region Overrides of Freezable    
        protected override Freezable CreateInstanceCore()
        {
            return new BindableConverter();
        }    
        #endregion       
    
        public TextBox SourceTextBox
        {
            get { return (TextBox)GetValue(SourceTextBoxProperty); }
            set { SetValue(SourceTextBoxProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for SourceTextBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceTextBoxProperty =
            DependencyProperty.Register("SourceTextBox", typeof(TextBox), typeof(BindableConverter), new PropertyMetadata(null));
    
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // ... do something with SourceTextBox here
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // ... do something
        }
    }