在代码隐藏中为 xaml 元素设置多重绑定
我有以下有效的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要来源:
You need sources:
直译是:
但是您可能会遇到 ElementName 正确解析的问题。在这种情况下,您必须直接绑定到该元素或“查找”它。如果 curveEditPoint 是当前类中的字段,则类似这样的操作会起作用:
The literal translation would be:
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:
尝试使用
而不是
Try to use
instead of