Legend Item Click

3 posts, 0 answers
  1. Nate East
    Nate East avatar
    4 posts
    Member since:
    Jul 2009

    Posted 18 Sep 2009 Link to this post

    Is there an event for a Legend Item Click like there is for Chart Item Click?

    Nate East
  2. Yusuf
    Yusuf avatar
    15 posts
    Member since:
    Jan 2009

    Posted 19 Sep 2009 Link to this post

    I also have a similiar requirement... I was thinking on lines of customizing the Chart layout and have this legends rendered as small buttons which can be clicked by the user... I am not sure if I am right track on this ?
  3. DevCraft banner
  4. Nate East
    Nate East avatar
    4 posts
    Member since:
    Jul 2009

    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
                        }
                    }
                }
            }
Back to Top