0

I'm using a 3rd party control (in this case, Telerik's RadChart control), which has a complex inner ControlTemplate that encompasses several elements of the chart. Unfortunately, not all parts are exposed explicitly for me to style, and I find myself wanting to set a property on a particular sub-element inside the ControlTemplate, either via XAML or via code. After struggling with the XAML for a while, I settled for a code-centric approach, but I'm still stuck.

Using Snoop, I can see that the top-most ChartArea control (which I can access) renders an AxisX2D object named PART_AxisX, which in turn has a template which contains the PART_AxisLabels object, which is the one I need to style. (Incidentally, the property I want to set is AlternationCount, but I couldn't find a less hacky way of getting to it).

Visual Tree

I tried to use the ControlTemplate.FindName method (as shown here) to get the AxisX2D object, and then again on that object to get the AxisLabels object, but FindName always returns null:

 var chartArea = myChart.DefaultView.ChartArea;
 var visualAxisX = chartArea.Template.FindName("PART_AxisX", chartArea) as AxisX2D;

even though Snoop confirms that chartArea is, in fact, the AxisX2D's TemplatedParent. I checked in the debugger and I could find PART_AxisX in chartArea's internal Template property.

So my questions are: 1) What am I doing wrong here? 2) Is there a better way to approach this that isn't as roundabout as this?

Community
  • 1
  • 1
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • Are you waiting till the chartArea is Loaded?...the Template might not have been applied yet. Should be done after OnAppyTemplate. – Colin Smith Aug 15 '12 at 13:25
  • Yes, that was indeed the problem. I was accessing it a bit too early, but I thought everything would be loaded by then. Thanks. Of course, Telerik's over-complicated visual tree made sure that this didn't, in fact help me after all. :) – Avner Shahar-Kashtan Aug 15 '12 at 13:27
  • Do you want me to turn it into an answer so that it can be ticked off ?...or convert comment to answer?...I don't know the etiquette. – Colin Smith Aug 15 '12 at 13:37

1 Answers1

1

The solution is to wait till after the Loaded event has occurred on the Control.

When this event has occurred you know that the Template has been applied and the visual tree has been built for the control, and so you can then access the elements using FindName.

Slightly related link given here for case when using a Content Template on a ContentControl.

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47