C# DataTemplate中RotateTransform角度上的绑定无效

C# DataTemplate中RotateTransform角度上的绑定无效,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,在我的WPF UserControl中,我有这个资源,请观察Xml示例中的注释 <UserControl.Resources> <DataTemplate x:Key="AxisXLabelTemplate"> <ContentPresenter Content="{Binding Path=Content}"> <ContentPresenter.LayoutTransform>

在我的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>
AxisXLabelTemplate数据模板分配给下面的元素,如下所示:

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); }
        }
...
}
<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>

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


有人能帮忙吗?

数据上下文是否正确?如果此循环是先前绑定到的
内容的一部分
,则需要更改
数据上下文
或将其添加到循环的绑定路径,即使其成为
{binding Content.XAxisLabelRotation}


你知道如何调试绑定吗?由于您没有显示任何绑定错误,我假设您没有,因此如果是这种情况,请阅读并确保您在将来无法调试绑定时提供了错误。

数据上下文是否正确?如果此循环是先前绑定到的
内容的一部分
,则需要更改
数据上下文
或将其添加到循环的绑定路径,即使其成为
{binding Content.XAxisLabelRotation}


你知道如何调试绑定吗?由于您没有出现任何绑定错误,我假设您没有,因此如果是这种情况,请阅读并确保您在将来无法自己调试绑定时提供了错误。

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

虽然如果我绑定工具提示

  <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}}" ..>
公共类角度转换器:IValueConverter
{
公共对象转换(…)
{
返回值;
}
....
}
它奇迹般地工作了


无法理解的WPF世界???

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

虽然如果我绑定工具提示

  <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}}" ..>
公共类角度转换器:IValueConverter
{
公共对象转换(…)
{
返回值;
}
....
}
它奇迹般地工作了


无法理解的WPF世界???

您需要像这样绑定角度:

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


您需要像这样绑定角度:

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


您是否查找了任何绑定错误(输出窗口)?您是否查找了任何绑定错误(输出窗口)?我所要做的就是添加ElementName=。。。使用我给想要绑定到的数据上下文的根元素指定的名称(在我的示例中,是元素所在的UserControl)。我猜元素中的RotateTransform的DataContext是不同的:\n无论哪种方式,在我的例子中都不需要虚拟转换器和UpdateSourceTrigger。我所要做的就是添加ElementName=。。。使用我给想要绑定到的数据上下文的根元素指定的名称(在我的示例中,是元素所在的UserControl)。我猜元素中的RotateTransform的DataContext是不同的:\n无论哪种方式,在我的例子中都不需要虚拟转换器和UpdateSourceTrigger。