Silverlight 4 Toolkit, Charting and LineSeries for NULL

I used the Silverlight 4 toolkit to create a Silverlight chart, which is released in April.

Please consider the following chart:

< /p>





So far so good. I can use MySuperChart .Series [0] Access the series in the chart but when I try to reference MyLineSeries, it seems to be null.
picture http://i41.tinypic.com/9k4h7t.png
Full view

< /div>

This is an interesting little question. If you have a little understanding of how to create and assign the variable MyLineSeries, it will help Navigate to the definition of the InitializeComponent method. You will end up in the file generated by MainPage.g.cs. It will contain this field: –

internal System.Windows .Controls.DataVisualization.Charting.LineSeries MyLineSeries;

In InitializeComponent you will find this line: –

this.MyLineSeries = ((System.Windows .Controls.DataVisualization.Charting.LineSeries)(this.FindName("MyLineSeries")));

Therefore, when the call to InitializeComponent in the constructor is completed, it should appear to have A value is assigned to MyLineSeries. But you can see that it is still null, so you can conclude that FindName(“MyLineSeries”) cannot find the series. So the question is why it failed?

Why does FindName not work?

FindName searches for the content of the “object tree” in the document and finds the object with the specified name (increase the complexity of the so-called name range, but it does not play a role here). Usually, the object is passed such as Public basic types such as Panel or ContentControl eventually appear in the “object tree”. These basic types have properties such as Children and Child, respectively. These properties are specified in the ContentProperty property of the class, allowing a more natural description of the UI structure. For example.:-

Replace

On the other hand, the Chart control is not a simple Panel derivative For the product, there is still a lot of work to do to build its UI. For Chart, ContentPropertyAttribute specifies the Series collection parameters. This allows you to have a more natural Xaml: –



< p>But because Chart has a lot of extra work to determine what it should be in the “object tree”, it will represent its final UI, the series collection items will not immediately become part of the “object tree”. Therefore, the InitializeComponent in FindName can’t find them at all.

Solution – Option 1

You can use the knowledge of the ordinal position of “MyLineSeries” in the chart to handle the assignment of the MyLineSeries variable in the constructor. From Delete x in Xaml: Name = “MyLineSeries” and then in the code: –

public partial MainPage: UserControl
{
private LineSeries MyLineSeries;

public MainPage()
{
InitializeComponent();
MyLineSeries = (LineSeries)MySuperChart.Series[0];
}
}

Solution – Option 2

You can wait until the series is in the “Object Available in the tree”, once the included UserControl has triggered its Loaded event, the series is true: –

public partial MainPage: UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += (s, args) =>
{
MyLineSeries = (LineSeries)FindName("MyLineSeries") ;
}
}
}

I used the Silverlight 4 toolkit to create a Silverlight chart, which will be released in April.

Please consider the following chart:





So far so good. I can access the series in the chart through MySuperChart.Series[0] but when I try to reference MyLineSeries, it seems to be null.< br>picture http://i41.tinypic.com/9k4h7t.png
Full view

This is an interesting little question. If you are right A little understanding of how to create and assign the variable MyLineSeries, it will help. Navigate to the definition of the InitializeComponent method. You will end up in the file generated by MainPage.g.cs. It will contain this field: –

internal System.Windows.Controls.DataVisualization.Charting.LineSeries MyLineSeries;

In InitializeComponent you will find this line: –

this.MyLineSeries = ((System.Windows.Controls.DataVisualization.Charting.LineSeries)(this.FindName("MyLineSeries")));

Therefore, the InitializeComponent When the call is completed, it seems that MyLineSeries should have been assigned a value. But you can see that it is still null, so you can conclude that FindName(“MyLineSeries”) cannot find the series. So the question is why it failed?

Why does FindName not work?

FindName searches for the content of the “object tree” in the document and finds the object with the specified name (increase the complexity of the so-called name range, but it does not play a role here). Usually, the object is passed such as Public basic types such as Panel or ContentControl eventually appear in the “object tree”. These basic types have properties such as Children and Child, respectively. These properties are specified in the ContentProperty property of the class, allowing a more natural description of the UI structure. For example.:-

Replace

On the other hand, the Chart control is not a simple Panel derivative For the product, there is still a lot of work to do to build its UI. For Chart, ContentPropertyAttribute specifies the Series collection parameters. This allows you to have a more natural Xaml: –



< p>But because Chart has a lot of extra work to determine what it should be in the “object tree”, it will represent its final UI, the series collection items will not immediately become part of the “object tree”. Therefore, the InitializeComponent in FindName can’t find them at all.

Solution – Option 1

You can use the knowledge of the ordinal position of “MyLineSeries” in the chart to handle the assignment of the MyLineSeries variable in the constructor. From Delete x in Xaml: Name = “MyLineSeries” and then in the code: –

publi c partial MainPage: UserControl
{
private LineSeries MyLineSeries;

public MainPage()
{
InitializeComponent();
MyLineSeries = ( LineSeries)MySuperChart.Series[0];
}
}

Solution-Option 2

You can wait until the series is available in the “Object Tree”, Once the containing UserControl has triggered its Loaded event, the series is true: –

public partial MainPage: UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += (s, args) =>
{
MyLineSeries = (LineSeries)FindName("MyLineSeries");
}
}
}

Leave a Comment

Your email address will not be published.