Is there any kind of event I can wire to in a Pie Chart that is fired when the LegendItems collection is updated?
I am creating a behavior I would like to attached to a PieChart and has a dependency property that will bind to the LegendItem like so:
Where PieChart is a RadPieChart and PieChartLegend is a RadLegend. I am using a behavior so I can project the Legend Items to a standard format that other charting objects are using with a common code set (so I dont have to write specific complex Styles/Templates for each). LegendItems on line 4 is a Dependency Property that calls a non-static method to wire the the CollectionChanged event:
This kind of works.
The data the chart is bound to can be changed by the user when they select different columns to chart. When they pick a new column, the dataset is recreated and the chart updates. But the LegendItems collection, it appears, is NOT recreated (= new LegendItemCollection) or emptied (.Clear()) but rather it has its item updated 1 to 1? Then extras are removed and new ones are added only when necessary? The result being that OnLegendItemCollectionOnCollectionChanged on line 21 isnt always tripped.
The reason I came to this conclusion is the CollectionChanged event fires ONLY when the number of items in LegendItems changes. If the user picks a field to map that results in the EXACT same number of legend items that the prior selected field resulted in the CollectionChanged event does NOT fire. I could use PropertyChanged on each LegendItem but that would result in the event being fired many times (it is already being called multiple times if the collection count changes but not nearly as much).
Is there something I am missing?
Thanks
Ernie
I am creating a behavior I would like to attached to a PieChart and has a dependency property that will bind to the LegendItem like so:
1.
<
i:Interaction.Behaviors
>
2.
<
localbehavior:RadPieChartLegendBehavior
3.
Legend
=
"{Binding ElementName=PieChartLegend}"
4.
LegendItems
=
"{Binding ElementName=PieChart, Path=LegendItems}"
/>
5.
</
i:Interaction.Behaviors
>
Where PieChart is a RadPieChart and PieChartLegend is a RadLegend. I am using a behavior so I can project the Legend Items to a standard format that other charting objects are using with a common code set (so I dont have to write specific complex Styles/Templates for each). LegendItems on line 4 is a Dependency Property that calls a non-static method to wire the the CollectionChanged event:
01.
public
ObservableCollection<LegendItem> LegendItems
02.
{
03.
get
{
return
(ObservableCollection<LegendItem>)GetValue(LegendItemsProperty); }
04.
set
{ SetValue(LegendItemsProperty, value); }
05.
}
06.
public
static
readonly
DependencyProperty LegendItemsProperty = DependencyProperty.Register(
07.
"LegendItems"
08.
,
typeof
(ObservableCollection<LegendItem>)
09.
,
typeof
(RadPieChartLegendBehavior)
10.
,
new
PropertyMetadata(
default
(ObservableCollection<LegendItem>), OnLegendItemCollectionChanged));
11.
12.
private
static
void
OnLegendItemCollectionChanged(DependencyObject dependencyObject
, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
13.
{
14.
var control = dependencyObject
as
RadPieChartLegendBehavior;
15.
if
(control !=
null
)
16.
control.WireLegendUpdate();
17.
}
18.
19.
private
void
WireLegendUpdate()
20.
{
21.
LegendItems.CollectionChanged += OnLegendItemCollectionOnCollectionChanged;
22.
}
This kind of works.
The data the chart is bound to can be changed by the user when they select different columns to chart. When they pick a new column, the dataset is recreated and the chart updates. But the LegendItems collection, it appears, is NOT recreated (= new LegendItemCollection) or emptied (.Clear()) but rather it has its item updated 1 to 1? Then extras are removed and new ones are added only when necessary? The result being that OnLegendItemCollectionOnCollectionChanged on line 21 isnt always tripped.
The reason I came to this conclusion is the CollectionChanged event fires ONLY when the number of items in LegendItems changes. If the user picks a field to map that results in the EXACT same number of legend items that the prior selected field resulted in the CollectionChanged event does NOT fire. I could use PropertyChanged on each LegendItem but that would result in the event being fired many times (it is already being called multiple times if the collection count changes but not nearly as much).
Is there something I am missing?
Thanks
Ernie