2016-12-16 92 views
0

我需要將兩個值(可能情況下命名爲單位和數量)作爲轉換器參數傳遞給綁定轉換器(請注意,我不需要將這些值作爲綁定值多重綁定),我需要將它們作爲綁定轉換器參數傳遞,因爲我需要轉換器的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」爲轉換器參數根據「_Quantity」和「_CurrentUnitEnum」將「_FluidBlackOilClass.SGGas_SC.Value」轉換爲新值並將其設置爲TextBoxText。還需要根據「_Quantity」和「_CurrentUnitEnum」將TextBoxText轉換回「_FluidBlackOilClass.SGGas_SC.Value」中。

+0

在輸出窗口任何約束力的錯誤? – WPFUser

+0

爲什麼將數量和單位綁定聲明爲TwoWay?這似乎沒有道理。你確定'_Quantity'和'_CurrentUnitEnum'是TextBoxUnitConvertor類的公共屬性嗎?下劃線看起來很奇怪。 – Clemens

+0

除此之外,真的很難理解這一切應該做什麼。你可能會試着解釋你實際想要達到的目標。也許我們可以提供更簡單的解決方案。 – Clemens

回答

1
  1. 讓你的轉換器繼承Freezable,並引入DP稱爲SourceTextBox將獲得關於你TextBox,然後在ConvertConvertBack方法,您可以使用此引用來得到所需的性能。

    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 
        } 
    } 
    
  2. 用法:

    <textboxunitconvertor:TextBoxUnitConvertor Name="gasDensityValueControl" ... > 
        <textboxunitconvertor:TextBoxUnitConvertor.Resources> 
         <local:BindableConverter x:Key="ValueStorageForUnitConverter" SourceTextBox="{Binding ElementName=gasDensityValueControl}"/> 
        </textboxunitconvertor:TextBoxUnitConvertor.Resources> 
        ... 
    </textboxunitconvertor:TextBoxUnitConvertor>