@arcticwhite did an amazing job helping me get to the point I am now in this post: Bind a DataGrid to the selectedrow object of a second datagrid, WPF Caliburn.Micro
I have been able to get it working with one small glitch. First, let me tell you what the app is supposed to do. I have a Datagrid that is populated with repair order information. Each repair order class has a list of "WriteOffs" (write-offs having their own class.
The app has a second Datagrid that shows all the write-offs for the selected repair order. Using arcticwhite's assistance I was able to get this working.
My problem is, when I add a new write-off to the selected repair order, it does not automatically show in the write-offs Datagrid.
In the ShellModelView (main window) this is the code for the collection that the write off Datagrid is bound to:
public BindableCollection<WriteOff> WriteOffs
{
get {
return GetWriteOffs();
}
}
The method call in the Write-offs collection
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; }
}
The below line returns a "List" if write offs - that's what the loop is for.
SelectedRepairOrder.GetMyWriteOffs()
The property the selected repair order is bound to:
public RepairOrder SelectedRepairOrder
{
get { return _selectedRepairOrder; }
set
{
if (_selectedRepairOrder == value) return;
_selectedRepairOrder = value;
NotifyOfPropertyChange(() => SelectedRepairOrder);
NotifyOfPropertyChange(() => WriteOffs);
}
}
The Repair order class:
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;
}
}
The Write Off Class:
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;
}
}
This is the method the "Add Write off button is bound too.
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());
}
What do I need to do to make the "Write Off" data grid automatically populate with the write off added? As it works now, if I select another line in the repair order grid and go back to the first, the new write off shows.
Me again :]
After calling WriteOffs.Add(tempWO);
Just add NotifyOfPropertyChange(() => WriteOffs);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.