2011-01-12 7 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を探しているビジュアルツリーを歩くことができます。マウスが上である項目のXとYの値はとき、左マウスプリントアウトする

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

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

このコードビハインド有する:

ここで、サンプルグラフのボタンをクリックします。

+0

私は少し複雑に見え、私は汚れて感じた:)私はデバッガで周りを突きつけ続け、良い見た目を見つけた。それから、あなたが持っているもののように少し見えることに気づいた。ここでそれはより短くて甘いですが、私はそれがすべきであるようにエレガントであるとは確信していません.... – GTAE86

関連する問題