WPF DataGrid - 如何自动退出编辑模式

WPF DataGrid - 如何自动退出编辑模式

在这里插入图片描述
这是一个数据输入和自动计算结果的表格,要实现的功能是输入载荷数据和示值数据自动计算出后面的各项结果,每个单元格使用CellEditEnding触发后进行计算,但在使用过程中发现了一个问题,在一个表格输入数据后,点击下方单元格触发正常,点击左侧或右侧单元格触发异常,读取刚编辑过得单元格数据位空,解决的办法是在CellEditEnding事件中添加一个定时器,触发定时器后在定时器中断里面执行CommitEdit()提交编辑操作,便可以正常触发CellEditEnding事件,读取到正常的数据,

下面是我使用的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using System.Windows.Threading;

namespace auto_calibrating
{
    /// <summary>
    /// Window_staticTest.xaml 的交互逻辑
    /// </summary>
    public partial class Window_staticTest : Window
    {
        public static readonly int DATA_LIST_NUM = 7;

        private List<Data_staticTest> staticTest_list;
        private DispatcherTimer timer_work;
        public bool e_less_5d = false;
        public Window_staticTest(bool e_less_5d_state)
        {
            InitializeComponent();
            this.Closing += Window_Closing;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;//设置窗口居中

            timer_work = new DispatcherTimer();
            timer_work.Interval = TimeSpan.FromMilliseconds(200);
            timer_work.Tick += timer_work_Tick;

            e_less_5d = e_less_5d_state;
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            dataGrid_input.CommitEdit();
            e.Cancel = false;
        }
        public void listVar_transmit(List<Data_staticTest> list)
        {
            staticTest_list = list;
            ((this.FindName("dataGrid_input")) as DataGrid).ItemsSource = staticTest_list;
            for (int i = 0; i < staticTest_list.Count; i++)
            {
                staticTest_list[i].Xvhao = (i + 1).ToString();
            }
            if (!e_less_5d)
            {
                dataGrid_input.Columns[4].IsReadOnly = true;
                dataGrid_input.Columns[4].Header = "";
                dataGrid_input.Columns[5].IsReadOnly = true;
                dataGrid_input.Columns[5].Header = "";
            }
        }

        private void dataGrid_input_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

            string str;
            str = (e.EditingElement as TextBox).Text;
            double result = 0;//定义值类型变量并赋值
            if (str == "")
            {
                timer_work.Start();
                return;
            }
            if (!double.TryParse(//判断数据是否为数值类型
                str, out result))
            {
                e.Cancel = true;//取消事件的值
                (e.EditingElement as TextBox).Text = "";
                MessageBox.Show("请输入数值");
            }
            else
            {
                (e.EditingElement as TextBox).Text = double.Parse(result.ToString("0.000")).ToString();
                timer_work.Start();
            }
        }
        private void data_calculate()
        {
            double E0;

            double e=10;
            double zaihe;
            double shizhi_1;
            double shizhi_2;
            double fujiazaihe_1;
            double fujiazaihe_2;
            double wucha_1;
            double wucha_2;
            double xiuzhengwucha_1;
            double xiuzhengwucha_2;

            for (int i = 0; i < DATA_LIST_NUM; i++)
            {
                staticTest_list[i].Wucha_1 = "";
                staticTest_list[i].Wucha_2 = "";
                staticTest_list[i].Xiuzhengwucha_1 = "";
                staticTest_list[i].Xiuzhengwucha_2 = "";
            }

            for (int i = 0; i < DATA_LIST_NUM; i++)
            {
                if ((staticTest_list[i].Zaihe != "") && (staticTest_list[i].Zaihe != null))
                {
                    if (e_less_5d)
                    {
                        if ((staticTest_list[i].Shizhi_1 != "") && (staticTest_list[i].Shizhi_1 != null))
                        {
                            if ((staticTest_list[i].Fujiazaihe_1 != "") && (staticTest_list[i].Fujiazaihe_1 != null))
                            {
                                zaihe = double.Parse(staticTest_list[i].Zaihe);
                                shizhi_1 = double.Parse(staticTest_list[i].Shizhi_1);
                                fujiazaihe_1 = double.Parse(staticTest_list[i].Fujiazaihe_1);
                                staticTest_list[i].Wucha_1 = (shizhi_1 - zaihe + 0.5 * e - fujiazaihe_1).ToString();
                            }
                        }
                        if ((staticTest_list[i].Shizhi_2 != "") && (staticTest_list[i].Shizhi_2 != null))
                        {
                            if ((staticTest_list[i].Fujiazaihe_2 != "") && (staticTest_list[i].Fujiazaihe_2 != null))
                            {
                                zaihe = double.Parse(staticTest_list[i].Zaihe);
                                shizhi_2 = double.Parse(staticTest_list[i].Shizhi_2);
                                fujiazaihe_2 = double.Parse(staticTest_list[i].Fujiazaihe_2);
                                staticTest_list[i].Wucha_2 = (shizhi_2 - zaihe + 0.5 * e - fujiazaihe_2).ToString();
                            }
                        }
                    }
                    else
                    {
                        if ((staticTest_list[i].Shizhi_1 != "") && (staticTest_list[i].Shizhi_1 != null))
                        {
                                zaihe = double.Parse(staticTest_list[i].Zaihe);
                                shizhi_1 = double.Parse(staticTest_list[i].Shizhi_1);
                                staticTest_list[i].Wucha_1 = (shizhi_1 - zaihe).ToString();
                        }
                        if ((staticTest_list[i].Shizhi_2 != "") && (staticTest_list[i].Shizhi_2 != null))
                        {
                                zaihe = double.Parse(staticTest_list[i].Zaihe);
                                shizhi_2 = double.Parse(staticTest_list[i].Shizhi_2);
                                staticTest_list[i].Wucha_2 = (shizhi_2 - zaihe).ToString();
                        }
                    }
                    if ((staticTest_list[0].Wucha_1 != "") && (staticTest_list[0].Wucha_1 != null))
                    {
                        E0 = double.Parse(staticTest_list[0].Wucha_1);
                        if ((staticTest_list[i].Wucha_1 != "") && (staticTest_list[i].Wucha_1 != null))
                        {
                            wucha_1 = double.Parse(staticTest_list[i].Wucha_1);
                            staticTest_list[i].Xiuzhengwucha_1 = (wucha_1 - E0).ToString();
                        }
                        if ((staticTest_list[i].Wucha_2 != "") && (staticTest_list[i].Wucha_2 != null))
                        {
                            wucha_2 = double.Parse(staticTest_list[i].Wucha_2);
                            staticTest_list[i].Xiuzhengwucha_2 = (wucha_2 - E0).ToString();
                        }
                    }
                }
            }

        }
        private void timer_work_Tick(object sender, EventArgs e)
        {
            timer_work.Stop();
            dataGrid_input.CommitEdit();
            data_calculate();
        }
    }

    public class Data_staticTest : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string xvhao;
        private string zaihe;
        private string shizhi_1;
        private string shizhi_2;
        private string fujiazaihe_1;
        private string fujiazaihe_2;
        private string wucha_1;
        private string wucha_2;
        private string xiuzhengwucha_1;
        private string xiuzhengwucha_2;
        public string Xvhao
        {
            get { return xvhao; }
            set
            {
                xvhao = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Xvhao"));
                }
            }
        }
        public string Zaihe
        {
            get { return zaihe; }
            set
            {
                zaihe = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Zaihe"));
                }
            }
        }
        public string Shizhi_1
        {
            get { return shizhi_1; }
            set
            {
                shizhi_1 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Shizhi_1"));
                }
            }
        }
        public string Shizhi_2
        {
            get { return shizhi_2; }
            set
            {
                shizhi_2 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Shizhi_2"));
                }
            }
        }
        public string Fujiazaihe_1
        {
            get { return fujiazaihe_1; }
            set
            {
                fujiazaihe_1 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Fujiazaihe_1"));
                }
            }
        }
        public string Fujiazaihe_2
        {
            get { return fujiazaihe_2; }
            set
            {
                fujiazaihe_2 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Fujiazaihe_2"));
                }
            }
        }
        public string Wucha_1
        {
            get { return wucha_1; }
            set
            {
                wucha_1 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Wucha_1"));
                }
            }
        }
        public string Wucha_2
        {
            get { return wucha_2; }
            set
            {
                wucha_2 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Wucha_2"));
                }
            }
        }
        public string Xiuzhengwucha_1
        {
            get { return xiuzhengwucha_1; }
            set
            {
                xiuzhengwucha_1 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Xiuzhengwucha_1"));
                }
            }
        }
        public string Xiuzhengwucha_2
        {
            get { return xiuzhengwucha_2; }
            set
            {
                xiuzhengwucha_2 = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Xiuzhengwucha_2"));
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值