Silverlight 4 toolkit, charting and lineSeries is null?

This is an interesting little gotcha. It helps if you look a bit under the hood at how the variable MyLineSeries is created and assigned. Navigate to the definition of the InitializeComponent method.

You will end up at the MainPage.g. Cs generated file. It will contain this field.

This is an interesting little gotcha. It helps if you look a bit under the hood at how the variable MyLineSeries is created and assigned. Navigate to the definition of the InitializeComponent method.

You will end up at the MainPage.g. Cs generated file. It will contain this field:- internal System.Windows.Controls. DataVisualization.Charting.

LineSeries MyLineSeries; and in the InitializeComponent you will find this line:- this. MyLineSeries = ((System.Windows.Controls. DataVisualization.Charting.

LineSeries)(this. FindName("MyLineSeries"))); So on the surface of it by the time the call to InitializeComponent in your constructor has completed the MyLineSeries should have been assigned a value. However as you can see its still null, hence it can be concluded that FindName("MyLineSeries") failed to find the series.

So the question is why did it fail? Why Doesn't FindName Work? FindName searches what is refered to in the documentation as the "object tree", looking for an object that has the name specified (there are added complications of what are known as namescopes but that is not at play here).

Typically objects end up in the "object tree" through common base types like Panel or ContentControl which have propeties such as Children and Child respectively. These properties are specified in the ContentProperty attribute on the classes which allows for the UI structure to be described more naturally.E.g. :- Instead of The Chart control, on the other hand, is not a simple Panel derivative and has a lot more work to do to build its UI.

In the case of the Chart the ContentPropertyAttribute specifies the Series collection parameter. This allows your more natural Xaml:- However because Chart has a lot of extra work in order do determine what exactly should be in the "object tree" that will represent its final UI the series collection items don't immediately become part of the "object tree". As result the FindName in the InitializeComponent simply doesn't find them.

Work Around - Option 1 You could use your knowledge of the ordinal position of "MyLineSeries" in the chart to handle the assignment of a MyLineSeries variable in the constructor. Remove the x:Name="MyLineSeries" from the Xaml then in code:- public partial MainPage : UserControl { private LineSeries MyLineSeries; public MainPage() { InitializeComponent(); MyLineSeries = (LineSeries)MySuperChart. Series0; } } Work Around - Option 2 You could wait until the series is available in the "object tree" which is true once the containing UserControl has fired its Loaded event:- public partial MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += (s, args) => { MyLineSeries = (LineSeries)FindName("MyLineSeries"); } } }.

I bow for you! – Snake May 3 '10 at 17:42 So you say it's kind of a bug? That they don't parse the x:Name?

– Snake May 3 '10 at 17:45 @Snake: The x:Name does get parsed but there is an assumption on the part of the auto-generated code that the element can be found in the "object tree" at the time InitializeComponent is executed. I'm not sure I would categorise this as a bug. – AnthonyWJones May 3 '10 at 18:29 @AnyhonyWJones: actually your workaround 2 does not work.It doesn't find the name.

– Snake May 4 '10 at 6:05 @Snake: My Bad, your right. I was writing from experience with SL3 and Nov09 toolkit. I've just tested the same in SL4 with April09 toolkit and the second option no longer works.

That's quite irritating. :( – AnthonyWJones May 4 '10 at 7:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions