none
WPF 如何获取点的坐标(只想问关键的一部,获取此时选中的点) RRS feed

  • 问题

  •  

    WPF 之前 画折线图,已经加入了datapoint ,有X,Y值;也同样加入了鼠标事件,现在就想知道怎样获取当前选中的点,以便后面编程。如下:

    private void dataPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                    DataPoint dataPoint = ( 这里面怎么书写,能够获取此刻鼠标单击选中的点 ,急切 );                    
                    MessageBox.Show(dataPoint.YValue.ToString());
            }

    2014年4月2日 2:23

答案

  • 你好,

    我不清楚你提到的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.

    2014年4月3日 2:46
    版主