开发者

C# WPF ChartingToolKit: How to get independent-axis value from ColumnSeries in MouseRightButtonDown?

I have a ColumnSeries with data bound to it and it displays correctly. When a column is right-clicked, I want to figure out what the independent-axis (X) value is. Ideally, I want to display a context menu with that information.

I have a MouseRightButtonDown handler, but cannot figure out how to perform the hit test to get the X-axis information.

开发者_StackOverflow中文版

I have selection enabled, but do not want to have to select a column prior to right-click.

Any help would be appreciated!


You can walk up the visual tree looking for a ColumnDataPoint.

Here's a sample graph:

<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
    <Grid.Resources>
        <PointCollection x:Key="sampleData">
            <Point X="1" Y="6"/>
            <Point X="2" Y="4"/>
            <Point X="3" Y="8"/>
        </PointCollection>
    </Grid.Resources>
    <chartingToolkit:Chart Title="Chart Title">
        <chartingToolkit:ColumnSeries Name="chart1" Title="Column Series" ItemsSource="{StaticResource sampleData}" IndependentValueBinding="{Binding X}" DependentValueBinding="{Binding Y}"/>
    </chartingToolkit:Chart>
</Grid>

and with this code-behind:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var element = Mouse.DirectlyOver as DependencyObject;
    while (element != null && !(element is ColumnDataPoint))
    {
        element = VisualTreeHelper.GetParent(element);
    }
    if (element != null)
    {
        var columnDataPoint = element as ColumnDataPoint;
        Debug.WriteLine("X = " + columnDataPoint.IndependentValue);
        Debug.WriteLine("Y = " + columnDataPoint.DependentValue);
    }
}

the X and Y values for the item the mouse is over will be printed out when the left mouse button is clicked.


Here is the example code that works.

MainWindow.xaml:

<Window x:Class="ColumnSeriesApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:ColumnSeriesApp"
      Title="Pet Data" Height="350" Width="525">

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColumnSeriesApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public PetData m_PetData;
    public MainWindow()
    {
        m_PetData = new PetData();
        DataContext = m_PetData;
        InitializeComponent();
    }

    private void m_colserHistogram_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Figure out what column we are on and display a popup menu based on the information.
        IInputElement ieMouseOver = e.MouseDevice.DirectlyOver;
        Rectangle rMouseOver = (Rectangle)ieMouseOver;
        string strMouseOverContext= rMouseOver.DataContext.ToString();
        string strMouseOverKey= "";
        foreach (var vKvP in m_PetData)
        {
            if (1 == strMouseOverContext.IndexOf(vKvP.Key))
                strMouseOverKey = vKvP.Key;
        }

        if (!String.IsNullOrEmpty(strMouseOverKey))
            MessageBox.Show("The X value is " + strMouseOverKey);
    }
}

public class PetData : Dictionary<string, int>
{
    public PetData()
    {
        Add("SallyBeagle", 7);
        Add("Cujo", 10);
        Add("DobyDeedle", 11);
        Add("Caramel", 6);
        Add("Boo", 6);
    }
}
}

It seems to work pretty well. If Rick had not come back with an idea, I would probably have quit looking for awhile - thanks for the motivation!

Now - is this solution all MVVM and whatnot? It still feels a little like a hack....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜