【问题标题】:Binding on RotateTransform Angle in DataTemplate not taking effect在 DataTemplate 中绑定 RotateTransform Angle 未生效
【发布时间】:2016-03-04 23:36:44
【问题描述】:

在我的 WPF UserControl 中我有这个资源,请观察 Xml 示例中的 cmets。

<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}"/>

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

有人可以帮忙吗?

【问题讨论】:

  • 您是否查找过任何绑定错误(输出窗口)?

标签: c# wpf xaml data-binding


【解决方案1】:

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

虽然如果我绑定工具提示

  <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 世界???

【讨论】:

  • 我所要做的就是添加 ElementName=... 以及我给要绑定到的数据上下文的根元素的名称(在我的情况下,我的元素所在的 UserControl在)。我猜元素内的 RotateTransform 的 DataContext 是不同的:\ 无论哪种方式,在我的情况下都不需要虚拟转换器和 UpdateSourceTrigger。
【解决方案2】:

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

你知道如何调试绑定吗?由于您没有出现任何绑定错误,我假设您没有,所以如果是这种情况,请阅读this article 并确保如果您以后无法自己调试绑定,您确实提供了错误。

【讨论】:

    【解决方案3】:

    你需要像这样绑定角度:

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

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 2011-12-30
      • 2012-07-14
      • 2017-08-26
      • 2011-05-11
      • 2017-02-18
      相关资源
      最近更新 更多