在我的应用程序中,我以编程方式生成数据网格并将其与列表绑定。我能够在datagrid中看到数据,但是当我编辑单元格时,列表中的基础项目不会更新。这是代码
Window dateChangeWindow = new Window();
dateChangeWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
dateChangeWindow.Owner = Application.Current.MainWindow;
dateChangeWindow.SizeToContent = SizeToContent.WidthAndHeight;
dateChangeWindow.Title = "Date Change";
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Vertical;
DataGrid requestGrid = new DataGrid();
requestGrid.CanUserResizeColumns = false;
requestGrid.CanUserResizeRows = false;
requestGrid.CanUserReorderColumns = false;
requestGrid.CanUserSortColumns = true;
requestGrid.AutoGenerateColumns = false;
DataGridTextColumn requestIdColumn = new DataGridTextColumn();
requestIdColumn.Header = "Request Id";
Binding idBinding = new Binding("RequestId");
idBinding.Mode = BindingMode.OneWay;
requestIdColumn.Binding = idBinding;
requestGrid.Columns.Add(requestIdColumn);
DataGridTemplateColumn startDateColumn = new DataGridTemplateColumn();
startDateColumn.Header = "Start Date";
Binding startDateBinding = new Binding("StartDate");
startDateBinding.Mode = BindingMode.TwoWay;
FrameworkElementFactory startDateFactory = new FrameworkElementFactory(typeof(DatePicker));
startDateFactory.SetBinding(DatePicker.SelectedDateProperty, startDateBinding);
DataTemplate startDateTemplate = new DataTemplate();
startDateTemplate.VisualTree = startDateFactory;
startDateColumn.CellTemplate = startDateTemplate;
startDateColumn.CellEditingTemplate = startDateTemplate;
requestGrid.Columns.Add(startDateColumn);
DataGridTemplateColumn endDateColumn = new DataGridTemplateColumn();
endDateColumn.Header = "End Date";
Binding endDateBinding = new Binding("EndDate");
endDateBinding.Mode = BindingMode.TwoWay;
FrameworkElementFactory endDateFactory = new FrameworkElementFactory(typeof(DatePicker));
endDateFactory.SetBinding(DatePicker.SelectedDateProperty, endDateBinding);
DataTemplate endDateTemplate = new DataTemplate();
endDateTemplate.VisualTree = endDateFactory;
endDateColumn.CellTemplate = endDateTemplate;
endDateColumn.CellEditingTemplate = endDateTemplate;
requestGrid.Columns.Add(endDateColumn);
requestGrid.ItemsSource = requestList;
requestGrid.Margin = new Thickness(0, 10, 0, 0);
requestGrid.HorizontalAlignment = HorizontalAlignment.Center;
stackPanel.Children.Add(requestGrid);
Button changeDoneBtn = new Button();
changeDoneBtn.Content = "Submit";
changeDoneBtn.Click += new RoutedEventHandler(changeDone_Click);
changeDoneBtn.Margin = new Thickness(0, 20, 0, 10);
changeDoneBtn.HorizontalAlignment = HorizontalAlignment.Center;
stackPanel.Children.Add(changeDoneBtn);
dateChangeWindow.Content = stackPanel;
dateChangeWindow.ShowDialog();
在创建窗口之前填充ItemsSource requestList
。它被声明为
IList<DateChangeWrapper> requestList = new List<DateChangeWrapper>();
DateChangeWrapper类看起来像这样
public class DateChangeWrapper : INotifyPropertyChanged
{
public DateChangeWrapper(ResponseWrapper responseWrapper)
{
RequestId = responseWrapper.RequestId;
ParentRequestId = responseWrapper.ParentRequestId;
StartDate = responseWrapper.StartDate;
EndDate = responseWrapper.EndDate;
}
private DateTime startDate;
private DateTime endDate;
public int RequestId { get; private set; }
public int ParentRequestId { get; private set; }
public DateTime StartDate
{
get { return startDate; }
set
{
startDate = value;
OnPropertyChanged("StartDate");
}
}
public DateTime EndDate
{
get { return endDate; }
set
{
endDate = value;
OnPropertyChanged("EndDate");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
从代码中可以看出,我有两列StartDate和EndDate显示为DatePickers。当我调试代码时,调用window.ShowDialog的点,我看到日期的get访问器被调用。但是当我更改datepicker中的值时,setter不会被调用,我的列表仍然具有旧值。
请帮帮我
答案 0 :(得分:0)
问题在于,由于UpdateSourceTrigger,更改后的值未更新回基础源。默认情况下,它具有LostFocus的值。我将其更改为PropertyChanged并且它有效! :)
我将以下代码行添加到我的绑定
startDateBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
,类似于EndDate