3

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); }
        }
...
}

The AxisXLabelTemplate DataTemplate is assigned to an element farther below as follows:

<chart:AxisLabel ElementTemplate="{StaticResource AxisXLabelTemplate}"/>

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.

Can anyone please help?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Jack
  • 1,477
  • 15
  • 20

3 Answers3

3

You need to bind the angle like this:

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

Source

Michele mpp Marostica
  • 2,445
  • 4
  • 29
  • 45
  • It works in my case (I was trying to template-bind RenderTransform Angle inside ControlTemplate). Looks like template binding does not apply to attached properties. – Kamil Sep 09 '22 at 21:28
3

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

WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • All I had to do was add ElementName=... with the name I gave to the root element of the data context I wanted to bind to (in my case, the UserControl that my element was residing in). I guess the DataContext is different for a RotateTransform inside an element :\ Either way, no need for the dummy converter and UpdateSourceTrigger in my case. – René Sackers May 21 '16 at 19:37
2

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400