在代码隐藏中为 xaml 元素设置多重绑定

发布于 2024-10-30 13:22:43 字数 2063 浏览 1 评论 0原文

我有以下有效的 XAML 代码,它基本上绑定了几个属性来计算用户控件的最终位置:

<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>

...

由于各种原因(但特别是为了避免相对源,我现在尝试在后面的代码中执行相同的操作,但没有成功。

这是我当前的代码:

    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);

    }

我仍然不敢相信将 XAML 转换为 C# 代码是如此困难。转换器仅被调用一次并且没有有效的属性值。

知道出了什么问题吗? 我该如何调试这样的问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-11-06 13:22:43

您需要来源:

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 });

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 });
鹿童谣 2024-11-06 13:22:43

直译是:

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 });

但是您可能会遇到 ElementName 正确解析的问题。在这种情况下,您必须直接绑定到该元素或“查找”它。如果 curveEditPoint 是当前类中的字段,则类似这样的操作会起作用:

multiBinding.Bindings.Add(new Binding("V") { Source = this.curveEditPoint });

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 });
向日葵 2024-11-06 13:22:43

尝试使用

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

而不是

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

Try to use

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

instead of

multiBinding.Bindings.Add(new Binding("CV.MinVProperty"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文