问题描述
在我的WPF UserControl中,我有此资源,请注意Xml示例中的注释。
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); }
}
...
}
AxisXLabelTemplate DataTemplate
The AxisXLabelTemplate DataTemplate is assigned to an element farther below as follows:
<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>
问题是,当我使用Angle的未绑定值并将其硬编码为-90时,它工作得很漂亮,当我尝试使用XAxisLabelRotation的绑定值时,它却没有。
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.
有人可以帮忙吗?
推荐答案
我重新创建了与您,它对我也不起作用。奇怪的是,没有绑定错误。我也做了relativesource绑定,但是没有用。
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.
虽然我绑定了工具提示
<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!!!
WPF无法解释的世界?
Unexplanable world of WPF???
这篇关于在DataTemplate中的RotateTransform Angle上的绑定未生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!