DataTemplate 中 RotateTransform 角度的绑定未生效
在我的 WPF UserControl 中,我有此资源,请观察 Xml 示例中的注释。
<UserControl.Resources>
<DataTemplate x:Key="AxisXLabelTemplate">
<ContentPresenter Content="{Binding Path=Content}">
<ContentPresenter.LayoutTransform>
<RotateTransform Angle="{Binding XAxisLabelRotation, UpdateSourceTrigger=PropertyChanged}" /> <!-- This does not work -->
<!-- <RotateTransform Angle="-90" /> This works -->
</ContentPresenter.LayoutTransform>
</ContentPresenter>
</DataTemplate>
</UserControl.Resources>
在代码中,我定义了一个依赖属性,如下所示:
class MyChart: INotifyPropertyChanged
{
public static readonly DependencyProperty XAxisLabelRotationProperty =
DependencyProperty.Register("XAxisLabelRotation", typeof(RotateTransform), typeof(BarChart),
new FrameworkPropertyMetadata((RotateTransform)new RotateTransform(-90.0)));
public RotateTransform XAxisLabelRotation
{
get { return (RotateTransform)GetValue(XAxisLabelRotationProperty); }
set { SetValue(XAxisLabelRotationProperty, value); }
}
...
}
AxisXLabelTemplate DataTemplate 被分配给下面的元素,如下所示:
<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>
问题是,当我使用角度的未绑定值并将其硬编码为 -90 时,它工作得很好,当我尝试使用 XAxisLabelRotation 的绑定值时,它没有。
有人可以帮忙吗?
In my WPF UserControl I have this resource, please observe the comments in the Xml sample.
<UserControl.Resources>
<DataTemplate x:Key="AxisXLabelTemplate">
<ContentPresenter Content="{Binding Path=Content}">
<ContentPresenter.LayoutTransform>
<RotateTransform Angle="{Binding XAxisLabelRotation, UpdateSourceTrigger=PropertyChanged}" /> <!-- This does not work -->
<!-- <RotateTransform Angle="-90" /> This works -->
</ContentPresenter.LayoutTransform>
</ContentPresenter>
</DataTemplate>
</UserControl.Resources>
In the code I have a dependency property defined as follows:
class MyChart: INotifyPropertyChanged
{
public static readonly DependencyProperty XAxisLabelRotationProperty =
DependencyProperty.Register("XAxisLabelRotation", typeof(RotateTransform), typeof(BarChart),
new FrameworkPropertyMetadata((RotateTransform)new RotateTransform(-90.0)));
public RotateTransform XAxisLabelRotation
{
get { return (RotateTransform)GetValue(XAxisLabelRotationProperty); }
set { SetValue(XAxisLabelRotationProperty, value); }
}
...
}
The AxisXLabelTemplate DataTemplate is assigned to an element farther below as follows:
<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>
The problem is that when I use the unbound value for Angle and hard-code it to -90, it works beautifully, and when I try with the bound value to XAxisLabelRotation it doesn't.
Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要像这样绑定角度:
来源
You need to bind the angle like this:
Source
我重新创建了与您类似的设置,但它也不适合我。奇怪的是没有绑定错误。我也做了相对源绑定,但没有用。
虽然如果我绑定工具提示
会向我显示工具提示。所以我改变了旋转变换,
但变换仍然不起作用。仍然没有绑定错误。
然后我介绍了一个虚拟转换器......
并且它奇迹般地工作了!
无法解释的WPF世界???
I recreated similar setting as you and it did not work for me either. Curiously there are no binding errors. I did relativesource binding too and it didnt work.
Although if I bind tooltip
shows the tooltip to me. So I changed the rotate transform,
But still the transform did not work. Still no binding errors.
Then I introduced a dummy converter ...
and it worked miraculously!!!
Unexplanable world of WPF???
DataContext
正确吗?如果此旋转是您之前绑定的Content
的一部分,您需要更改DataContext
或将其添加到旋转的绑定路径中,即使其{绑定Content.XAxisLabelRotation}
。您知道如何调试绑定吗?由于您没有提出任何绑定错误,我假设您没有,所以如果是这种情况,请阅读 本文 并确保在无法调试绑定时提供错误未来的自己。
Is the
DataContext
correct? If this rotation is part of theContent
you bound to earlier you either need to change theDataContext
or add that to the binding path of the rotation, i.e. make it{Binding Content.XAxisLabelRotation}
.Do you know how to debug bindings? As you did not present any binding errors i would assume you do not, so if this is the case please read this article and make sure that you do provide the errors if you cannot debug a binding yourself in the future.