在WPF LineSeries DataPoint上添加事件

时间:2011-07-22 16:55:23

标签: c# .net wpf xaml wpftoolkit

我正在使用WPF Toolkit绘制折线图(我们的应用程序中的一个功能)。给定一个Collection,我能够绘制图形,但是,当用户双击图形上的DataPoint时,我发现很难获得X和Y数据值(而不是线图中的Co-Ordinate值) )。

我可以使用DataPointStyle设置属性,但无法向其添加事件。

如果我在LineSeries节点上使用MouseDoubleClick="lineChart_ShowResults_DoubleClick"属性,那么当用户点击任何一点时它会触发一个事件。但是,如果用户点击DataPoint,我需要触发事件。以下是我尝试实现的XAML。请帮忙。

<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 个答案:

答案 0 :(得分:3)

正如您所说,事件会在点击任何点时触发,因为事件已分配给LineSeries。在这一行(来自你的帖子)

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

通过进入LineSeries.DataPointStyle你是在正确的道路,但我相信你应该定义一个事件setter而不是setter。 像这样:

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

显然,删除了LineSeries上的事件处理。

我没试过,让我知道它是否有效