开发者

Set multibinding for a xaml element in code behind

开发者 https://www.devze.com 2023-02-22 02:30 出处:网络
I have the following working XAML code that basically binds a couple of properties to calculate the final position of my user control:

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开发者_C百科 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 });


Try to use

multiBinding.Bindings.Add(new Binding("MinVProperty") { ElementName = CV.Name });

instead of

multiBinding.Bindings.Add(new Binding("CV.MinVProperty"));
0

精彩评论

暂无评论...
验证码 换一张
取 消