自动更新数据网格 - WPF C#Caliburn Micro

时间:2018-02-05 02:26:38

标签: c# wpf xaml caliburn.micro

@arcticwhite做了一个了不起的工作,帮助我达到了我现在在这篇文章中的观点: Bind a DataGrid to the selectedrow object of a second datagrid, WPF Caliburn.Micro

我已经能够使用一个小故障。首先,让我告诉你应用程序应该做什么。我有一个填充了修复订单信息的Datagrid。每个修理订单类都有一个“WriteOffs”列表(注销有自己的类。

该应用程序有第二个Datagrid,显示所选修复订单的所有注销。使用arcticwhite的帮助,我能够实现这一目标。

我的问题是,当我向选定的修理订单添加新的注销时,它不会自动显示在注销Datagrid中。

在ShellModelView(主窗口)中,这是写关闭Datagrid绑定的集合的代码:

public BindableCollection<WriteOff> WriteOffs
        {
            get {

                return GetWriteOffs();
            }

        }

Write-offs集合中的方法调用

public BindableCollection<WriteOff> GetWriteOffs()
        {
            BindableCollection<WriteOff> temp = new BindableCollection<WriteOff>();
            if (SelectedRepairOrder != null)
            {
                if (SelectedRepairOrder.GetMyWriteOffs() != null)
                {
                    foreach (var item in SelectedRepairOrder.GetMyWriteOffs())
                    {
                        temp.Add(item);

                    }
                    return temp;
                }
                else { return null; }
            }
            else { return null; }
        }

如果注销,下面一行会返回一个“List” - 这就是循环的用途。

SelectedRepairOrder.GetMyWriteOffs()

所选修理订单绑定的属性:

public RepairOrder SelectedRepairOrder
        {
            get { return _selectedRepairOrder; }
            set
            {
               if (_selectedRepairOrder == value) return;
                _selectedRepairOrder = value;
                NotifyOfPropertyChange(() => SelectedRepairOrder);
                NotifyOfPropertyChange(() => WriteOffs);

            }
        }

修理订单类:

public class RepairOrder
    {

        public string ControlNumber { get; set; }
        public double Value { get; set; }
        public string Note { get; set; }
        public string Schedule { get; set; }
        public int Age { get; set; }
        private List<WriteOff> _myWriteOffs;

        public List<WriteOff> GetMyWriteOffs()
        {

            return _myWriteOffs;
        }

        public void AddMyWriteOff(WriteOff value)
        { _myWriteOffs.Add(value); }


        public RepairOrder(string CN, string SC, double VL)
        {
            ControlNumber = CN;
            Schedule = SC;
            Value = Math.Round(VL, 2);
            Note = null;
            _myWriteOffs = new List<WriteOff>();
        }

        public RepairOrder()
        {
            _myWriteOffs = new List<WriteOff>();
        }

        public static RepairOrder FromCSV(string CSVLine, string Sched)
        {
            string[] values = CSVLine.Split(',');
            RepairOrder rep = new RepairOrder();
            rep.ControlNumber = values[2];
            rep.Value = Math.Round(double.Parse(values[5]),2);
            rep.Age = int.Parse(values[4]);
            rep.Schedule = Sched;
            return rep;
        }



    }

注销课程:

public class WriteOff
    {
        public string Account { get; set; }
        public string Description { get; set; }
        public double WriteOffAmount { get; set; }

        public WriteOff(string Acct, string Desc, double Amount)
        {
            Account = Acct;
            Description = Desc;
            WriteOffAmount = Amount;
        }
    }

这是“添加注销按钮也绑定了”的方法。

double WOA;
            if (SelectedAccount == null)
            {
                MessageBox.Show("Please select a Write off Account, Description and amount", "Incorrect Write Off Information", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (SelectedWODescription == null)
            {
                MessageBox.Show("Please select a Write off Account, Description and amount", "Incorrect Write Off Information", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (WriteOffAmount == null)
            {
                MessageBox.Show("Please select a Write off Account, Description and amount", "Incorrect Write Off Information", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                WOA = Double.Parse(WriteOffAmount);
                if (WOA == 0)
                {
                    MessageBox.Show("Write off can not be $0.00", "Incorrect Write Off Amount", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            //Check if there is a selected repair order
            if (SelectedRepairOrder == null)
            {
                MessageBox.Show("Please select a Repair Order to write this off to", "No RO Selected", MessageBoxButton.OK);
                return;
            }

            WOA = Double.Parse(WriteOffAmount);
            WriteOff tempWO = new WriteOff(SelectedAccount, SelectedWODescription, WOA);
            SelectedRepairOrder.AddMyWriteOff(tempWO);
            WriteOffs.Add(tempWO);
            //MessageBox.Show(SelectedAccount + Environment.NewLine + SelectedWODescription + Environment.NewLine + WriteOffAmount, "Test", MessageBoxButton.OK);
            //MessageBox.Show(tempWO.Account + Environment.NewLine + tempWO.Description + Environment.NewLine + tempWO.WriteOffAmount, ToString());

        }

我需要做什么才能自动填充“注销”数据网格并添加注销?现在可以使用,如果我在修复订单网格中选择另一行并返回第一行,则会显示新的注销。

1 个答案:

答案 0 :(得分:1)

我再次:]

致电WriteOffs.Add(tempWO);

只需添加NotifyOfPropertyChange(() => WriteOffs);

即可