传递多个绑定值作为绑定 CONVERTER PARAMETER
pass multiple binded values as binding CONVERTER PARAMETER
我需要将两个值(在可能的情况下命名为 Unit 和 Quantity)作为转换器参数传递给绑定转换器。(请注意,我不需要将这些值作为绑定值(多重绑定)传递,我需要将它们作为绑定转换器参数传递,因为我需要转换器的 Convert
和 ConvertBack
方法)。
我认为可行的唯一方法是创建一个新的 class UnitQuantityBindClass 以将它们设置为 class 并将此 class 作为转换器传递参数,但此 class 没有获得绑定值,当我通过转换器时,创建的 class 转换器参数没有值。
有人可以帮助我吗?
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”中。
让你的 Converter 继承 Freezable
,并引入一个名为 SourceTextBox
的 DP,它将获取对你的 TextBox
的引用,然后在你的 Convert
和 ConvertBack
方法,您可以使用此引用来获取所需的属性。
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
}
}
用法:
<textboxunitconvertor:TextBoxUnitConvertor Name="gasDensityValueControl" ... >
<textboxunitconvertor:TextBoxUnitConvertor.Resources>
<local:BindableConverter x:Key="ValueStorageForUnitConverter" SourceTextBox="{Binding ElementName=gasDensityValueControl}"/>
</textboxunitconvertor:TextBoxUnitConvertor.Resources>
...
</textboxunitconvertor:TextBoxUnitConvertor>
我需要将两个值(在可能的情况下命名为 Unit 和 Quantity)作为转换器参数传递给绑定转换器。(请注意,我不需要将这些值作为绑定值(多重绑定)传递,我需要将它们作为绑定转换器参数传递,因为我需要转换器的 Convert
和 ConvertBack
方法)。
我认为可行的唯一方法是创建一个新的 class UnitQuantityBindClass 以将它们设置为 class 并将此 class 作为转换器传递参数,但此 class 没有获得绑定值,当我通过转换器时,创建的 class 转换器参数没有值。
有人可以帮助我吗?
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”中。
让你的 Converter 继承
Freezable
,并引入一个名为SourceTextBox
的 DP,它将获取对你的TextBox
的引用,然后在你的Convert
和ConvertBack
方法,您可以使用此引用来获取所需的属性。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 } }
用法:
<textboxunitconvertor:TextBoxUnitConvertor Name="gasDensityValueControl" ... > <textboxunitconvertor:TextBoxUnitConvertor.Resources> <local:BindableConverter x:Key="ValueStorageForUnitConverter" SourceTextBox="{Binding ElementName=gasDensityValueControl}"/> </textboxunitconvertor:TextBoxUnitConvertor.Resources> ... </textboxunitconvertor:TextBoxUnitConvertor>