Adding events on WPF LineSeries DataPoint

2019-02-26 03:59发布

问题:

I am using WPF Toolkit to draw a line chart (a feature on our application). Given a Collection, I am able to plot the graph, however, when the user double clicks on the DataPoint on the graph, I am finding it difficult to get the X and Y data value (not the Co-Ordinate value in the line graph).

I am able to set property using DataPointStyle, but unable to add event to it.

If I use MouseDoubleClick="lineChart_ShowResults_DoubleClick" property on the LineSeries node, then it triggers an event when user clicks on any point. But, I need to trigger the event only if the user clicks on the DataPoint. The following is the XAML that I tried to implement. Please help.

<Window x:Class="TeamXXX.YYYUI.GraphicalDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GraphicalDisplay" Height="400" Width="600" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="439" d:DesignWidth="654" SizeToContent="WidthAndHeight">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Grid MinHeight="360" MinWidth="575" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <chartingToolkit:Chart Name="lineChart" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <chartingToolkit:Chart.LegendStyle>
                <Style TargetType="Control">
                    <Setter Property="Height" Value="0" />
                    <Setter Property="Width" Value="0" />
                </Style>
            </chartingToolkit:Chart.LegendStyle>
            <chartingToolkit:LineSeries DependentValuePath="Value" Name="lineSeries" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" MouseDoubleClick="lineChart_ShowResults_DoubleClick">

                <!--<chartingToolkit:LineSeries.DataPointStyle>
                    <Style x:Uid="CommonLineSeriesDataPoint" TargetType="chartingToolkit:LineDataPoint">
                        <Setter Property="" Property="lineChart_ShowResults_DoubleClick"/>
                    </Style>
                </chartingToolkit:LineSeries.DataPointStyle>-->

                <chartingToolkit:LineSeries.DependentRangeAxis>
                    <chartingToolkit:LinearAxis Orientation="Y" Title="Cost in minutes" FontSize="16" />
                </chartingToolkit:LineSeries.DependentRangeAxis>
                <chartingToolkit:LineSeries.IndependentAxis>
                    <chartingToolkit:LinearAxis Orientation="X" Title="Fold" FontSize="16" />
                </chartingToolkit:LineSeries.IndependentAxis>
            </chartingToolkit:LineSeries>
        </chartingToolkit:Chart>
    </Grid>
</ScrollViewer>
</Window>

回答1:

As you said, The event triggers upon a click on any of the points because the event is assigned to the LineSeries. On this line (from your post)

<chartingToolkit:LineSeries DependentValuePath="Value" Name="lineSeries" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" MouseDoubleClick="lineChart_ShowResults_DoubleClick">

You were in the right path by going into LineSeries.DataPointStyle but I believe that you should define an event setter instead of a setter. Like this:

<chartingToolkit:LineSeries.DataPointStyle>
    <Style>
        <EventSetter>
            <EventSetter Event="Control.MouseDoubleClick" Handler="lineChart_ShowResults_DoubleClick"/>     
        </EventSetter>      
    </Style> </chartingToolkit:LineSeries.DataPointStyle>
</chartingToolkit:LineSeries.DataPointStyle>

And obviously remove the event handling on the LineSeries.

I did not try it, let me know if it works