问题
This is not so much a question that I am stuck on, but a question as to if there is a better way. It works as it stands, but I want to get a better understand if I could.
Why do I always have to update the DataGrid.ItemSource when I am using an ObservableCollection<T> for binding?
I am binding the ObservableCollection<T> to the DataGrid using the below code.
public partial class MainWindow : INotifyPropertyChanged
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
CalcObservable =
DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key, DateFilter);
MyDataGrid.ItemsSource = CalcObservable;
}
public ObservableCollection<CalcTable> CalcObservable { get; set; }
= new ObservableCollection<CalcTable>();
}
And this is the function that gets the data from the DataBase,
internal class DatabaseQueries
{
public static ObservableCollection<CalcTable> ShiftInputSourceObserv(int staffNo, DateTime date)
{
using (DatabaseDataContext dataContext = new DatabaseDataContext(MainWindow.InstanceConnectionString))
{
return new ObservableCollection<CalcTable>
(dataContext.CalcTables.Where(
p => p.Staff_No == staffNo &&
p.Year_No == date.Year &&
p.Month_No == date.Month)
.OrderBy(p => p.Column_Index));
}
}
}
I then use a ComboBox change event to update the ObservableCollection<T>. SelectedEmployee.Key is bound to the ComboBox and changes the selected employee upon selection:
private void NumbersComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CalcObservable =
DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key, DateFilter);
MyDataGrid.ItemsSource = CalcObservable;
}
I was under the impression that changing the ObservableCollection<T> would update WITHOUT having to use the MyDataGrid.ItemsSource = CalcObservable; line again?
Thanks for the help.
回答1:
I am binding the
ObservableCollection<T>to theDataGridusing the below code ...
No, you don't bind it. You set the ItemsSource property to the value of your CalcObservable property.
You are then setting the CalcObservable property to a new ObservableCollection<T> in your NumbersComboBox_SelectionChanged event handler. This won't somehow automatically update the ItemsSource property of the DataGrid.
If you actually do bind to the CalcObservable property you could refresh the DataGrid provided that your class implements the INotifyPropertyChanged interface and you raise the PropertyChanged event in the setter of the CalcObservable property:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
CalcObservable = DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key, DateFilter);
MyDataGrid.SetBinding(ComboBox.ItemsSourceProperty, new Binding(nameof(CalcObservable)) { Source = this });
}
private ObservableCollection<CalcTable> _calcObservable = new ObservableCollection<CalcTable>();
public ObservableCollection<CalcTable> CalcObservable
{
get { return _calcObservable; }
set { _calcObservable = value; OnPropertyChanged(nameof(CalcObservable)); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
The other option is to clear the existing instance of the ObservableCollection<T> and add new items to it instead of creating a new Collection each time you want to update the DataGrid.
来源:https://stackoverflow.com/questions/42135024/change-out-observablecollection-upon-combobox-change