首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在DataTemplate中绑定RotateTransform角度不起作用

在DataTemplate中绑定RotateTransform角度不起作用
EN

Stack Overflow用户
提问于 2011-09-26 05:46:55
回答 3查看 3.8K关注 0票数 1

在我的WPF UserControl中,我有这个资源,请注意Xml示例中的注释。

代码语言:javascript
复制
<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>

在代码中,我有一个依赖属性,定义如下:

代码语言:javascript
复制
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被分配给下面更远的元素,如下所示:

代码语言:javascript
复制
<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>

问题是,当我为Angle使用未绑定的值并将其硬编码为-90时,它工作得很好,而当我尝试将绑定值设置为XAxisLabelRotation时,它却不能。

有人能帮帮忙吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-26 15:33:34

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

尽管如果我绑定工具提示

代码语言:javascript
复制
  <ContentPresenter ToolTip="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

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

代码语言:javascript
复制
  <RotateTransform Angle="{Binding XAxisLabelRotation, 
                                      RelativeSource={RelativeSource
                                          AncestorType={x:Type ContentControl}},
                                             UpdateSourceTrigger=PropertyChanged}" ..>

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

然后我引入了一个虚拟转换器。

代码语言:javascript
复制
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的不可解释的世界?

票数 3
EN

Stack Overflow用户

发布于 2011-09-26 05:58:32

DataContext是否正确?如果此循环是您之前绑定到的Content的一部分,则需要更改DataContext或将其添加到循环的绑定路径中,即将其设置为{Binding Content.XAxisLabelRotation}

你知道如何调试绑定吗?因为您没有出现任何绑定错误,所以如果是这样的话,请阅读this article,并确保在将来不能自己调试绑定时提供错误。

票数 2
EN

Stack Overflow用户

发布于 2016-03-04 23:36:45

您需要像这样绑定角度:

代码语言:javascript
复制
<RotateTransform Angle="{Binding Path=FontSize, RelativeSource={RelativeSource TemplatedParent}}"/>

Source

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7548849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档