Reputation: 63
I need to pass two values (in may case named Unit and Quantity) as the converter parameter to a binding converter.(Note that I do not need to pass these values as binding values (multibinding), I need to pass them as binding converter parameter because, I need both Convert
and ConvertBack
methods of converter).
The only way that I was thought that works was to create a new class UnitQuantityBindClass to set them in that class and pass this class as converter parameter but this class does not get bind values and when I go through the converter, the converter parameter , which is the created class, does not have values.
can an one help me on this?
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); }
}
}
Usage:
<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>
Note: my requirement is to get "_FluidBlackOilClass.SGGas_SC.Value" and pass it to the converter and also i need to pass "_Quantity" and "_CurrentUnitEnum" as the converters parameter to convert the "_FluidBlackOilClass.SGGas_SC.Value" according to the "_Quantity" and "_CurrentUnitEnum" to a new value and set it as TextBoxText. also i need to convertback the TextBoxText to store in the "_FluidBlackOilClass.SGGas_SC.Value" according to the "_Quantity" and "_CurrentUnitEnum".
Upvotes: 1
Views: 979
Reputation: 9827
Make your Converter inherit Freezable
, and introduce a DP called SourceTextBox
which will get the reference to your TextBox
, then in your Convert
and ConvertBack
methods, you can use this reference to get the needed properties.
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
}
}
Usage :
<textboxunitconvertor:TextBoxUnitConvertor Name="gasDensityValueControl" ... >
<textboxunitconvertor:TextBoxUnitConvertor.Resources>
<local:BindableConverter x:Key="ValueStorageForUnitConverter" SourceTextBox="{Binding ElementName=gasDensityValueControl}"/>
</textboxunitconvertor:TextBoxUnitConvertor.Resources>
...
</textboxunitconvertor:TextBoxUnitConvertor>
Upvotes: 2