I have the following working XAML code that basically binds a couple of properties to calculate the final position of my user control:
<UserControl x:Class="CurvePointControl"
....
>
<UserControl.Resources>
<local:VToYConverter x:Key="vToYConverter" />
</UserControl.Resources>
<UserControl.RenderTransform>
<TranslateTransform x:Name="XTranslateTransform" >
<TranslateTransform.Y>
<MultiBinding Converter="{StaticResource vToYConverter}">
<Binding ElementName="curveEditPoint" Path="V"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:CurveEditor}}" Path="MinV"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:CurveEditor}}" Path="MaxV"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:CurveEditor}}" Path="ActualHeight"/>
</MultiBinding>
</TranslateTransform.Y>
</TranslateTransform>
</UserControl.RenderTransform>
...
For various reasons (but esp to avoid the relative source, I am now trying to do the same in code behind without success.
This is my current code:
public CurvePointControl(CurveEditor CV)
{
InitializeComponent();
MultiBinding multiBinding = new MultiBinding();
multiBinding.Converter = m_VToYConverter;
multiBinding.Bindings.Add(new Binding("V"));
multiBinding.Bindings.Add(new Binding(CV.MinVProperty)); // doesn't work
multiBinding.Bindings.Add(new Binding(CV.MaxVProperty)); // doesn't work
multiBinding.Bindings.Add(new Binding(CV.ActualHeight)); // doesn't work
multiBinding.NotifyOnSourceUpdated= true;
this.SetBinding(TranslateTransform.YProperty, multiBinding);
//Doesn't work too:
//BindingOperations.SetBinding(XTranslateTransform, TranslateTransform.YProperty, multiBinding);
}
I still can't believe that it is so hard to convert the XAML into c# code. The converter is called but only once and without valid property values.
Any idea of what's wrong? How could I debug such a problem?
You need sources:
multiBinding.Bindings.Add(new Binding("V") { Source = curveEditPoint }); //If that object is accessible in the current scope.
multiBinding.Bindings.Add(new Binding("MinV") { Source = CV });
multiBinding.Bindings.Add(new Binding("MaxV") { Source = CV });
multiBinding.Bindings.Add(new Binding("ActualHeight") { Source = CV });
The literal translation would be:
MultiBinding multiBinding = new MultiBinding();
multiBinding.Converter = m_VToYConverter;
RelativeSource relativeSource = new RelativeSource() { AncestorType = typeof(CurveEditor) };
multiBinding.Bindings.Add(new Binding("V") { ElementName = "curveEditPoint" });
multiBinding.Bindings.Add(new Binding(CV.MinVProperty) { RelativeSource = relativeSource });
multiBinding.Bindings.Add(new Binding(CV.MaxVProperty) { RelativeSource = relativeSource });
multiBinding.Bindings.Add(new Binding(CV.ActualHeight) { RelativeSource = relativeSource });
But you may run into problems with the ElementName resolving correctly. In that case you'd have to bind directly to the element or "find" it. Something like this would work if curveEditPoint is a field in the current class:
multiBinding.Bindings.Add(new Binding("V") { Source = this.curveEditPoint });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With