DataTemplate 中 RotateTransform 角度的绑定未生效

发布于 2024-12-06 13:41:11 字数 1509 浏览 0 评论 0原文

在我的 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 技术交流群。

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

发布评论

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

评论(3

つ可否回来 2024-12-13 13:41:11

您需要像这样绑定角度:

<RotateTransform Angle="{Binding Path=FontSize, RelativeSource={RelativeSource TemplatedParent}}"/>

来源

You need to bind the angle like this:

<RotateTransform Angle="{Binding Path=FontSize, RelativeSource={RelativeSource TemplatedParent}}"/>

Source

々眼睛长脚气 2024-12-13 13:41:11

我重新创建了与您类似的设置,但它也不适合我。奇怪的是没有绑定错误。我也做了相对源绑定,但没有用。

虽然如果我绑定工具提示

  <ContentPresenter ToolTip="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

会向我显示工具提示。所以我改变了旋转变换,

  <RotateTransform Angle="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

但变换仍然不起作用。仍然没有绑定错误。

然后我介绍了一个虚拟转换器......

public class AngleConverter : IValueConverter
{
    public object Convert(...)
    {
        return value;
    }
    ....
}

<RotateTransform Angle="{Binding XAxisLabelRotation, 
                                 RelativeSource={RelativeSource
                                      AncestorType={x:Type ContentControl}},
                                 UpdateSourceTrigger=PropertyChanged,
                                 Converter={StaticResource AngleConverter}}" ..>

并且它奇迹般地工作了!

无法解释的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

  <ContentPresenter ToolTip="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

shows the tooltip to me. So I changed the rotate transform,

  <RotateTransform Angle="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

But still the transform did not work. Still no binding errors.

Then I introduced a dummy converter ...

public class AngleConverter : IValueConverter
{
    public object Convert(...)
    {
        return value;
    }
    ....
}

<RotateTransform Angle="{Binding XAxisLabelRotation, 
                                 RelativeSource={RelativeSource
                                      AncestorType={x:Type ContentControl}},
                                 UpdateSourceTrigger=PropertyChanged,
                                 Converter={StaticResource AngleConverter}}" ..>

and it worked miraculously!!!

Unexplanable world of WPF???

小情绪 2024-12-13 13:41:11

DataContext 正确吗?如果此旋转是您之前绑定的 Content 的一部分,您需要更改 DataContext 或将其添加到旋转的绑定路径中,即使其 {绑定Content.XAxisLabelRotation}

您知道如何调试绑定吗?由于您没有提出任何绑定错误,我假设您没有,所以如果是这种情况,请阅读 本文 并确保在无法调试绑定时提供错误未来的自己。

Is the DataContext correct? If this rotation is part of the Content you bound to earlier you either need to change the DataContext 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.

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