在 WPF LineSeries DataPoint 上添加事件

发布于 2024-11-25 13:07:55 字数 2847 浏览 0 评论 0原文

我正在使用 WPF Toolkit 绘制折线图(我们应用程序的一项功能)。给定一个集合,我可以绘制图表,但是,当用户双击图表上的数据点时,我发现很难获取 X 和 Y 数据值(而不是折线图中的坐标值) )。

我可以使用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-12-02 13:07:55

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

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

您进入 LineSeries.DataPointStyle 的路径是正确的,但我相信您应该定义一个事件设置器而不是设置器。
像这样:

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

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

我没有尝试过,请告诉我是否有效

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文