none
WPF System.Windows.Controls.DataVisualization.Charting.LineSeries 的SelectionChanged事件有时会不触发? RRS feed

  • 问题

  • WPFToolkit 中 System.Windows.Controls.DataVisualization.Charting.LineSeries 的SelectionChanged事件有时会不触发。连续点不同的点,多点几次(不固定)就会出现这个问题。我用的是WPF Toolkit\v3.5.50211.1。windows7

    源码如下:

    MainWindow.xaml

    <Window x:Class="chart2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="777" Width="928" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="43,504,0,0" Name="textBox1" VerticalAlignment="Top" Width="309" />
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="463,575,0,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click" />
            <TabControl Height="412" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="861">
                <TabItem Header="tabItem1" Name="tabItem1"></TabItem>
            </TabControl>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="584,575,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button5_Click" />
        </Grid>
    </Window>

    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;
    using System.Data;
    using System.Windows.Controls.DataVisualization.Charting;

    namespace chart2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
              
            }
          
            private void LineSeries_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                textBox1.Text = ((System.Data.DataRowView)((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).SelectedItem)[0].ToString() + "-" + ((System.Data.DataRowView)((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).SelectedItem)[((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).Tag.ToString()].ToString();
            }

            private void button4_Click(object sender, RoutedEventArgs e)
            {
                Random random = new Random();
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("time_stamp", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("para_value1", typeof(decimal)));
                dt.Columns.Add(new DataColumn("para_value2", typeof(decimal)));
                DataRow dr = null;
                for (int i = 0; i <= 10; i++)
                {
                    dr = dt.NewRow();
                    dr["time_stamp"] = DateTime.Now.AddDays(i);
                    dr["para_value1"] = random.Next(1, 100);
                    dr["para_value2"] = random.Next(1, 100);
                    dt.Rows.Add(dr);
                }
                           
                string[] a = new string[2];
                a[0] = "para_value1";
                a[1] = "para_value2";
                System.Windows.Controls.DataVisualization.Charting.Chart chart = MainWindow.GetLineChart(dt, true, "time_stamp", a);
                for (int i = 0; i < chart.Series.Count; i++)
                {
                    ((System.Windows.Controls.DataVisualization.Charting.DataPointSeries)chart.Series[i]).SelectionChanged += new SelectionChangedEventHandler(LineSeries_SelectionChanged);
                }
              
                tabItem1.Content = chart;
            }

            private void button5_Click(object sender, RoutedEventArgs e)
            {
                Random random = new Random();
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("time_stamp", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("para_value1", typeof(decimal)));
                dt.Columns.Add(new DataColumn("para_value2", typeof(decimal)));
                DataRow dr = null;
                for (int i = 0; i <= 10; i++)
                {
                    dr = dt.NewRow();
                    dr["time_stamp"] = DateTime.Now.AddDays(i);
                    dr["para_value1"] = random.Next(1, 100);
                    dr["para_value2"] = random.Next(1, 100);
                    dt.Rows.Add(dr);
                }
                string[] a = new string[2];
                a[0] = "para_value1";
                a[1] = "para_value2";
                System.Windows.Controls.DataVisualization.Charting.Chart chart = MainWindow.GetColumnChart(dt, true, "time_stamp", a);
                for (int i = 0; i < chart.Series.Count; i++)
                {
                    ((System.Windows.Controls.DataVisualization.Charting.ColumnSeries)chart.Series[i]).SelectionChanged += new SelectionChangedEventHandler(LineSeries_SelectionChanged);
                }
                tabItem1.Content = chart;
               
               
            }

            public static Chart GetLineChart(DataTable dataTable, bool isMultiCoordinates, string axis_X, string[] axis_Y)
            {
                Chart chart = new Chart();
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    LineSeries lineSeries;
                    LinearAxis linearAxis;
                    CategoryAxis xAxis = new CategoryAxis { Orientation = AxisOrientation.X, Title = "(年)" };
                    for (int i = 0; i < axis_Y.Length; i++)
                    {
                        lineSeries = new LineSeries();
                        lineSeries.DependentValuePath = axis_Y[i];
                        lineSeries.IndependentValuePath = axis_X;
                        lineSeries.ItemsSource = dataTable.DefaultView;
                        lineSeries.Tag = axis_Y[i];
                        lineSeries.Title = dataTable.Columns[axis_Y[i]].Caption;
                        lineSeries.IsSelectionEnabled = true;
                        if (isMultiCoordinates)
                        {
                            linearAxis = new LinearAxis();
                            linearAxis.Orientation = AxisOrientation.Y;
                            dataTable.DefaultView.Sort = string.Format("{0} ", axis_Y[i]);
                            linearAxis.Maximum = Math.Ceiling(double.Parse(dataTable.DefaultView[dataTable.Rows.Count - 1][axis_Y[i]].ToString()) / 10) * 10;
                            linearAxis.Minimum = Math.Floor(double.Parse(dataTable.DefaultView[0][axis_Y[i]].ToString()) / 10) * 10;
                            linearAxis.Interval = (linearAxis.Maximum - linearAxis.Minimum) / 10;
                            linearAxis.ShowGridLines = true;
                            linearAxis.Title = dataTable.Columns[axis_Y[i]].Caption;
                            lineSeries.DependentRangeAxis = linearAxis;
                            lineSeries.IndependentAxis = xAxis;
                        }
                        chart.Series.Add(lineSeries);
                    }
                    dataTable.DefaultView.Sort = "";
                }
                return chart;
            }

            public static Chart GetColumnChart(DataTable dataTable, bool isMultiCoordinates, string axis_X, string[] axis_Y)
            {
                Chart chart = new Chart();
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    ColumnSeries columnSeries;
                    LinearAxis linearAxis;
                    CategoryAxis xAxis = new CategoryAxis { Orientation = AxisOrientation.X, Title = "(年)" };
                    for (int i = 0; i < axis_Y.Length; i++)
                    {
                        columnSeries = new ColumnSeries();
                        columnSeries.DependentValuePath = axis_Y[i];
                        columnSeries.IndependentValuePath = axis_X;
                        columnSeries.ItemsSource = dataTable.DefaultView;
                        columnSeries.Tag = axis_Y[i];
                        columnSeries.Title = dataTable.Columns[axis_Y[i]].Caption;
                        columnSeries.IsSelectionEnabled = true;
                        if (isMultiCoordinates)
                        {
                            linearAxis = new LinearAxis();
                            linearAxis.Orientation = AxisOrientation.Y;
                            dataTable.DefaultView.Sort = string.Format("{0} ", axis_Y[i]);
                            linearAxis.Maximum = Math.Ceiling(double.Parse(dataTable.DefaultView[dataTable.Rows.Count - 1][axis_Y[i]].ToString()) / 10) * 10;
                            linearAxis.Minimum = Math.Floor(double.Parse(dataTable.DefaultView[0][axis_Y[i]].ToString()) / 10) * 10;
                            linearAxis.Interval = (linearAxis.Maximum - linearAxis.Minimum) / 20;
                            linearAxis.ShowGridLines = true;
                            linearAxis.Title = dataTable.Columns[axis_Y[i]].Caption;
                            columnSeries.DependentRangeAxis = linearAxis;
                            columnSeries.IndependentAxis = xAxis;
                        }
                        chart.Series.Add(columnSeries);
                    }
                    dataTable.DefaultView.Sort = "";
                }
                return chart;
            }
          

          
          
        }
    }



    • 已编辑 肖铖 2012年2月18日 10:00
    2012年2月17日 9:41

答案

  • 我重现了,并不是连续点不同的点,而是一次不断点上下相同日期的点,她就不再触发SelectionChanged。因为你有两条LineSeries,他们分别有各自的SelectedItem,当你已经选中这个,再上下一次点击同一个选中项,是不会触发各自的SelectionChanged事件的。

    所以你可以额外再增加一个一个事件:

    ((System.Windows.Controls.DataVisualization.Charting.DataPointSeries)chart.Series[i]).PreviewMouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_PreviewMouseLeftButtonDown);

    然后:

        void MainWindow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          if (((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).SelectedItem != null)
            textBox1.Text =
              ((System.Data.DataRowView)((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).SelectedItem)[0].ToString()
              + "-" + ((System.Data.DataRowView)((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).SelectedItem)[((System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes)sender).Tag.ToString()].ToString();
        }
    

    需要保留原有的SelectionChanged,因为PreviewMouseLeftButtonDown比SelectionChanged先触发,第一次可能造成没有选中项就触发了ButtonDown。

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 肖铖 2012年2月21日 1:16
    2012年2月20日 2:56
    版主