Silverlight: spatial reference issue
I am using Visifire diagrams to display data in a Windows Phone 7 application. I created a diagram that was properly bound to a dependency property. It worked great. I decided to turn the chart into a custom control since I was going to use it in another project and also with the same setup. Now my data binding doesn't work unless I bind it in code, not XAML.
here's what i have:
<UserControl ... x:Name="root">
...
<chart:DataSeries ... DataSource="{Binding ElementName=root, Path=Results}">
...
</UserControl>
and the code behind:
public MyList Results
{
get { return (MyList)GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
// Using a DependencyProperty as the backing store for Results. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResultsProperty =
DependencyProperty.Register("Results", typeof(MyList), typeof(MyChart), new PropertyMetadata(null));
public GoogleChart()
{
Loaded += delegate
{
// theChart.Series[0].DataSource = Results;
};
Results = new GoogleResults();
InitializeComponent();
}
If I uncomment the line theChart.Series[0].DataSource = Results;
it works fine. But if I leave that line commented out (for example, before I moved the diagram to the UserControl), it doesn't link. (By the way: theChart
is the x:name
parent of the chart, so the first item .Series[0]
refers to the chart).
Does anyone know why this is happening? Again, it worked great until I moved the code to UserControl.
thanks
a source to share
If I understand you correctly, you created this UserControl to place it on different pages in your application.
In this case, you will most likely give these instances a name. This name will replace the "Root" name that is originally assigned in the UserControl Xaml. Hence, binding for ElementName=Root
will fail.
Usually there is a root element (usually a grid) named "LayoutRoot". Therefore, instead of relying on the UserControl's name to change, use "LayoutRoot", which is by convention the Content element for the UserControl. Like this: -
<chart:DataSeries ... DataSource="{Binding ElementName=LayoutRoot, Path=Parent.Results}">
Note that the property path now starts with Parent
, which takes you to UserControl
, not finally requiring you to know the UserControl name.
a source to share