gpt4 book ai didi

c# - 在代码隐藏中为 xaml 元素设置多重绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 22:28:31 24 4
gpt4 key购买 nike

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

<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# 代码如此困难。转换器仅被调用一次,并且没有有效的属性值。

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

最佳答案

你需要资源:

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

关于c# - 在代码隐藏中为 xaml 元素设置多重绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com