在ComboBox更改后更改ObservableCollection [英] Change out ObservableCollection upon ComboBox change
问题描述
为什么我总是要更新当我使用
ObservableCollection< T>
进行绑定时,DataGrid.ItemSource
我使用以下代码将 ObservableCollection< T>
绑定到 DataGrid
。
public partial class MainWindow:INotifyPropertyChanged
{
public MainWindow()
{
DataContext =这个;
初始化组件();
CalcObservable =
DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key,DateFilter);
MyDataGrid.ItemsSource = CalcObservable;
}
public ObservableCollection< CalcTable> CalcObservable {get;组; }
= new ObservableCollection< CalcTable>();
}
这是从DataBase获取数据的功能,
内部类DatabaseQueries
{
public static ObservableCollection< CalcTable> ShiftInputSourceObserv(int staffNo,DateTime date)
{
using(DatabaseDataContext dataContext = new DatabaseDataContext(MainWindow.InstanceConnectionString))
{
返回新的ObservableCollection< CalcTable>
(dataContext.CalcTables.Where(
p => p.Staff_No == staffNo&&b
p.Year_No == date.Year&&
p。 Month_No == date.Month)
.OrderBy(p => p.Column_Index));
}
}
}
然后我使用code> ComboBox 更改事件以更新 ObservableCollection< T>
。 SelectedEmployee.Key
绑定到 ComboBox
,并在选择时更改所选员工:
private void NumbersComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
CalcObservable =
DatabaseQueries.ShiftInputSourceObserv(SelectedEmployee.Key,DateFilter );
MyDataGrid.ItemsSource = CalcObservable;
}
我的印象是,更改 ObservableCollection< T>
将更新 WITHOUT 必须再次使用 MyDataGrid.ItemsSource = CalcObservable;
行?
感谢您的帮助。
我将
ObservableCollection< T>
绑定到DataGrid
使用以下代码...
不,你不绑定它。您将 ItemsSource
属性设置为 CalcObservable
属性的值。
然后,您将 CalcObservable
属性设置为新 ObservableCollection< T>
你的 NumbersComboBox_SelectionChanged
事件处理程序。这不会以某种方式自动更新 DataGrid
的 ItemsSource
属性。
如果您实际上将绑定绑定到 CalcObservable
属性,则可以刷新 DataGrid
如果您的类实现了 INotifyPropertyChanged
接口,并且您在<$ c的设置器中引发 PropertyChanged
事件$ c> 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));
}
}
另一个选项是清除现有的 ObservableCollection< T>
并添加新项目,而不是每次要更新DataGrid时创建一个新的集合。
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.
I am binding the
ObservableCollection<T>
to theDataGrid
using 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.
这篇关于在ComboBox更改后更改ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!