2

I'm using the RadChart from Telerik in WPF and I have a problem when I want to specify the MaxValue, MinValue and Step for an additional Y-axis.

It works fine if I hardcode the values like this (the relevant part is the last four lines):

<Controls:AxisY MinorTicksVisibility="Hidden" 
    DefaultLabelFormat="#VAL{#,##0}" 
    Title="{UI:Language @{xCubicMetrePerHourSymbol}}" 
    MajorGridLinesVisibility="Hidden" StripLinesVisibility="Visible" 
    AxisName="CubicMetreAxis" 
    AutoRange="False" 
    MaxValue="10000" 
    MinValue="0" 
    Step="0">

But since I want to change the MinValue, MaxValue and Step depending on the values in the series, I have to bind it this way using properties from a ViewModel object (which is supposed to be my inherited DataContext):

<Controls:AxisY MinorTicksVisibility="Hidden" 
    DefaultLabelFormat="#VAL{#,##0}" 
    Title="{UI:Language @{xCubicMetrePerHourSymbol}}" 
    MajorGridLinesVisibility="Hidden" StripLinesVisibility="Visible" 
    AxisName="CubicMetreAxis" 
    AutoRange="False" 
    MaxValue="{Binding Path=MyMaxValue}" 
    MinValue="{Binding Path=MyMinValue}"
    Step="{Binding Path=MyStep}">

But it seems that the DataContext cannot be seen / set from the additional Y-axis from what I've gathered using Snoop.

I get the following error message (the same message for each of the bindings):

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or 
FrameworkContentElement for target element. BindingExpression:Path=MyMaxValue;   
DataItem=null; target element is 'AxisY' (HashCode=47300984); target property is 
'MaxValue' (type 'Double')

I thought I had made a mistake somewhere and have searched for hours, but I discovered something weird: if I use those bindings in the "original" Y-axis, everything works perfectly. The code is exactly the same for both axes. The only difference is in the parent elements.

This works:

<Controls:ChartArea.AxisY>
    <Controls:AxisY MinorTicksVisibility="Hidden" 
                    DefaultLabelFormat="#VAL{#,##0}" 
                    Title="{UI:Language @{GigaJoulePerHourSymbol}}" 
                    MajorGridLinesVisibility="Hidden" 
                    StripLinesVisibility="Visible" 
                    AutoRange="False" 
                    MaxValue="{Binding Path=MyMaxValue}" 
                    MinValue="{Binding Path=MyMinValue}" 
                    Step="{Binding Path=MyStep}">
        <Controls:AxisY.AxisStyles>
            <Controls:AxisStyles TitleStyle="{StaticResource TranslatedAxisStyle}" />
        </Controls:AxisY.AxisStyles>
    </Controls:AxisY>
</Controls:ChartArea.AxisY>

This doesn't:

<Controls:ChartArea.AdditionalYAxes>
    <Controls:AxisY MinorTicksVisibility="Hidden" 
                    DefaultLabelFormat="#VAL{#,##0}" 
                    Title="{UI:Language @{xCubicMetrePerHourSymbol}}" 
                    MajorGridLinesVisibility="Hidden" 
                    StripLinesVisibility="Visible"
                    AxisName="CubicMetrePerHourAxis"  
                    AutoRange="False" 
                    MaxValue="{Binding Path=MyMaxValue}" 
                    MinValue="{Binding Path=MyMinValue}" 
                    Step="{Binding Path=MyStep}">
        <Controls:AxisY.AxisStyles>
            <Controls:AxisStyles TitleStyle="{StaticResource TranslatedAxisStyle}" />
        </Controls:AxisY.AxisStyles>
    </Controls:AxisY>
</Controls:ChartArea.AdditionalYAxes>

The element itself is exactly the same, except that the second one has a name and the title is different, but I tested and it doesn't actually affect anything. The only difference is that the parent elements are different.

I'd like to know why the AxisY sees the DataContext and can bind properly while the AdditionalYAxis cannot.

Jumbala
  • 4,764
  • 9
  • 45
  • 65
  • Probably (I'm guessing) because `AdditionalYAxes` is not in the visual tree. See if you can apply one of the standard workarounds, eg: http://stackoverflow.com/a/5647426/1001985 – McGarnagle Nov 22 '13 at 23:07
  • @McGarnagle It works! Thanks a lot, I would never have found out how to do it. Post this as an answer if you want me to accept it. – Jumbala Nov 26 '13 at 13:36

1 Answers1

1

Since AdditionalYAxis is presumably not in the visual tree, it will not inherit its logical-tree parent's DataContext. That explains why binding is not working in the normal way. You can use a workaround like the one outlined here ("data context spy") to get the binding working correctly.

Community
  • 1
  • 1
McGarnagle
  • 101,349
  • 31
  • 229
  • 260