在我的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}"/>问题是,当我为Angle使用未绑定的值并将其硬编码为-90时,它工作得很好,而当我尝试将绑定值设置为XAxisLabelRotation时,它却不能。
有人能帮帮忙吗?
发布于 2011-09-26 15:33:34
我重新创建了与你类似的设置,它对我也不起作用。奇怪的是,没有绑定错误。我也做了相关的资源绑定,但它不起作用。
尽管如果我绑定工具提示
<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的不可解释的世界?
发布于 2011-09-26 05:58:32
DataContext是否正确?如果此循环是您之前绑定到的Content的一部分,则需要更改DataContext或将其添加到循环的绑定路径中,即将其设置为{Binding Content.XAxisLabelRotation}。
你知道如何调试绑定吗?因为您没有出现任何绑定错误,所以如果是这样的话,请阅读this article,并确保在将来不能自己调试绑定时提供错误。
发布于 2016-03-04 23:36:45
您需要像这样绑定角度:
<RotateTransform Angle="{Binding Path=FontSize, RelativeSource={RelativeSource TemplatedParent}}"/>https://stackoverflow.com/questions/7548849
复制相似问题