我有一个ColumnSeries,数据绑定到它,它显示正确。右键单击一列时,我想弄清楚独立轴(X)的值是什么。理想情况下,我想显示包含该信息的上下文菜单。
我有一个MouseRightButtonDown处理程序,但无法弄清楚如何执行命中测试以获取X轴信息。
我启用了选择功能,但不希望在右键单击之前选择一列。
任何帮助将不胜感激!
答案 0 :(得分: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值。
答案 1 :(得分: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还有什么?它仍然感觉有点像黑客......