Hi *,
I use a Chart Control (see below)
Items is a collection of Objects with different properties like X, Y, Z and each item has an Id.
My question is how can I get the value of the ID Property having a selected Item (DataPoint) by SelectedValue = "X"?
All what I have is an object of type SelectionChangedEventArgs contains AddedPoints [SelectionChanged] but there I see only the values I render in the Chart.
Please help.
Thx
R
<
telerik:BarSeries
ShowLabels
=
"True"
ValueBinding
=
"{Binding ElementName=radListBox, Path=SelectedValue}"
ItemsSource
=
"{Binding ElementName=ChartDataSource1}"/
>
...
<telerik:ChartDataSource x:Name="ChartDataSource1"
ItemsSource="{Binding Items ...
5 Answers, 1 is accepted
In order to get the view model of the DataPoint you can use its DataItem property.
var myViewModel = e.AddedPoints[0].DataItem
as
MyViewModel;
var id = myViewModel.ID;
I hope this information is useful.
Regards,
Martin
Telerik
Martin, thx for the answer, but in my case this property is null
new System.Collections.Generic.Mscorlib_CollectionDebugView<Telerik.Charting.DataPoint>(e.AddedPoints).Items[0].DataItem Value : null
Am I missing something?
Thanks
Rostislaw
The DataItem property will have a value only if the series' ItemsSource is populated. Otherwise, it will be null. I attached a sample project which demonstrates how you can get the DataItem property. Also, you can take a look at the Create Data-Bound Chart help article.
Regards,
Martin
Telerik
Martin,
Thank you for
the example. In your sample app the data item property is not null (as
expected).
I found out
what the problem is but I do not know if this is an expected behavior.
Working
code (data item is valid):
<telerik:BarSeries ShowLabels="True"
CategoryBinding="Id"
ValueBinding="{Binding SelectedChart}"
ItemsSource="{Binding Items}">
</telerik:BarSeries>
Code with data
item null:
<telerik:ChartDataSource x:Name="ChartDataSource1" ItemsSource="{Binding Items}" />
…
</telerik:BarSeries>
<telerik:BarSeries ShowLabels="True"
CategoryBinding="Id"
ValueBinding="{Binding SelectedChart}"
ItemsSource="{Binding ElementName=ChartDataSource1}">
</telerik:BarSeries>
It looks like the indirection for the ItemSource in XAML does not provide this information.
Anyway for
me the problem is solved, thank you for the excellent support.
Rostislaw
This behavior is expected with RadChartView and its sampling source. ChartDataSource samples the original items which means that it combines several data items into one and the DataItem property doesn't know which one to use. This is why it is empty. However, we have a feature request for adding support for this scenario. You can find it in our feedback portal where you can track its status and vote for its implementation.
Regards,
Martin
Telerik