Posted 21 Sep 2009
Link to this post
So what I ended up doing was modifying the Legend Style. I put a button in with an opacity of 0 to make it not visible, but still clickable. You have to create this style and set the chart to use it in code:
_chart.DefaultView.ChartLegend.LegendItemStyle = this.CustomLegendStyle;
You'll notice that I bind the Tag property of the button to my label property for use in code. The eventhandler code is at the bottom.
<Style x:Name="CustomLegendStyle" TargetType="chart:ChartLegendItem">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="chart:ChartLegendItem">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Rectangle x:Name="PART_LegendItemMarker"
Height="15"
Width="30"
StrokeThickness="1"
RadiusX="2"
RadiusY="2"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Margin="0,0,0,5"
Style="{TemplateBinding ItemStyle}" />
<TextBlock
Grid.Column="1"
Padding="0"
Margin="5,5,5,7"
VerticalAlignment="Center"
Height="14"
Foreground="White"
Text="{TemplateBinding Label}" />
<Button x:Name="btnLegendLabel"
Margin="0,2,0,2"
Grid.ColumnSpan="2"
Opacity="0"
Cursor="Hand"
Tag="{TemplateBinding Label}"
Click="btnLegendLabel_Click"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
//Kinda specific to my use case, but I hope you can see what I'm doing here.
private void btnLegendLabel_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn.Tag.ToString() != "Requirements")
{
//Loop through all the dataseries to find the dataseries click on.
foreach (DataSeries ds in _chart.DefaultView.ChartArea.DataSeries)
{
if (ds.LegendLabel == btn.Tag.ToString())
{
//Do Something Here
}
}
}
}