![](/img/trans.png)
[英]How to pass collection to binding converter parameter in silverlight
[英]pass multiple binded values as binding CONVERTER PARAMETER
我需要将两个值(可能称为“单位”和“数量”)作为转换参数传递给绑定转换器。(请注意,我不需要将这些值作为绑定值(多重绑定)传递,我需要将它们作为绑定转换器传递参数,因为我需要Convert
器的Convert
和ConvertBack
方法)。 我认为可行的唯一方法是创建一个新类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”设置为新值,并将其设置为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.