2011-01-12 27 views
2

我有一個數據綁定到它的ColumnSeries,它顯示正確。當右鍵單擊列時,我想知道獨立軸(X)值是多少。理想情況下,我想顯示帶有這些信息的上下文菜單。C#WPF ChartingToolKit:如何在MouseRightButtonDown中從ColumnSeries獲取獨立軸值?

我有一個MouseRightButtonDown處理程序,但無法弄清楚如何執行命中測試來獲取X軸信息。

我已啓用選擇,但不希望在右鍵單擊之前選擇列。

任何幫助,將不勝感激!

回答

0

下面是可用的示例代碼。

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); 
    } 
} 
} 

這似乎工作得很好。如果瑞克沒有回來的想法,我可能會放棄尋找一段時間 - 感謝動機!

現在 - 這個解決方案全部是MVVM和什麼?它仍然感覺有點像一個黑客....

1

您可以在視覺樹上尋找ColumnDataPoint

這裏的一個示例圖表:

<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> 

和與該代碼隱藏:

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); 
    } 
} 

的X和Y值處的項目的鼠標在將被打印輸出的鼠標左鍵時按鈕被點擊。

+0

看起來有點複雜,讓我覺得骯髒:)我一直在調試器周圍戳,發現一些看起來不錯。然後我發現它看起來有點像你的一些東西。在這裏,它更短,更甜,但我不確定它是否應該如此優雅.... – GTAE86 2011-01-13 17:12:08

相關問題