Binding on RotateTransform Angle in DataTemplate not taking effect

前端 未结 3 1686
旧巷少年郎
旧巷少年郎 2021-01-27 01:11

In my WPF UserControl I have this resource, please observe the comments in the Xml sample.


    

        
相关标签:
3条回答
  • 2021-01-27 01:35

    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.

    Although if I bind tooltip

      <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!!!

    Unexplanable world of WPF???

    0 讨论(0)
  • 2021-01-27 01:56

    Is the DataContext correct? If this rotation is part of the Content you bound to earlier you either need to change the DataContext or add that to the binding path of the rotation, i.e. make it {Binding Content.XAxisLabelRotation}.

    Do you know how to debug bindings? As you did not present any binding errors i would assume you do not, so if this is the case please read this article and make sure that you do provide the errors if you cannot debug a binding yourself in the future.

    0 讨论(0)
  • 2021-01-27 01:57

    You need to bind the angle like this:

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

    Source

    0 讨论(0)
提交回复
热议问题