积极答复者
WPF 如何获取点的坐标(只想问关键的一部,获取此时选中的点)

问题
答案
-
你好,
我不清楚你提到的DataPoint是什么,在WPF Chart 中,应该是LineDataPoint
如果是按照我的方法创建的折线图的,可以为LineDataPoint指定 PreviewMouseLeftButtonDown 事件处理:
XAML:
xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" Title="MainWindow" Height="350" Width="525"> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Grid.Column="1" Grid.Row="0"> <charting:Chart x:Name="waitingtasks" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Stretch" Title="Waiting Tasks"> <charting:Chart.TitleStyle> <Style TargetType="datavis:Title"> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Foreground" Value="White"/> </Style> </charting:Chart.TitleStyle> <charting:Chart.LegendStyle> <Style TargetType="datavis:Legend"> <Setter Property="Width" Value="0" /> </Style> </charting:Chart.LegendStyle> <charting:Chart.Axes> <charting:LinearAxis Orientation="X" Title="Time" Interval="20" ShowGridLines="False"/> <charting:LinearAxis Title="Number of Waiting Tasks" Orientation="Y" Interval="5" ShowGridLines="False"/> </charting:Chart.Axes> <charting:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" AnimationSequence="FirstToLast" > <charting:LineSeries.DataPointStyle> <Style TargetType="charting:LineDataPoint"> <EventSetter Event="Control.PreviewMouseLeftButtonDown" Handler="LineDataPoint_PreviewMouseLeftButtonDown"/> </Style> </charting:LineSeries.DataPointStyle> </charting:LineSeries> </charting:Chart> </Grid>
后台:
ObservableCollection<KeyValuePair<double, double>> Power = new ObservableCollection<KeyValuePair<double, double>>(); public MainWindow() { InitializeComponent(); Power.Add(new KeyValuePair<double, double>(1, 0.4)); Power.Add(new KeyValuePair<double, double>(2, 0.2)); Power.Add(new KeyValuePair<double, double>(3, 0.5)); Power.Add(new KeyValuePair<double, double>(4, 0.8)); Power.Add(new KeyValuePair<double, double>(5, 0.1)); Power.Add(new KeyValuePair<double, double>(6, 0.3)); Power.Add(new KeyValuePair<double, double>(7, 0.7)); waitingtasks.DataContext = Power; } private void LineDataPoint_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { LineDataPoint dataPoint = (LineDataPoint)sender; MessageBox.Show("X: "+dataPoint.IndependentValue.ToString()+" Y: "+dataPoint.DependentValue.ToString()); }
截图:
如果你使用的是第三方控件,可以去相应的三方产品论坛提问。We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已建议为答案 Leon_Chang 2014年4月9日 5:41
- 已标记为答案 Franklin ChenMicrosoft employee, Moderator 2014年4月9日 9:09