Good morning! I'm trying to bind a XamDataGrid to an ObservableCollection made up of an object derived from a DBContext generated with EntityFramework. Though I am binding the datagrid's Data Source to my Observable Collection, the grid never visually changes, and I'd like to know if I'm missing something. I know that the Observable Collection is populating, so I must be doing something wrong with the Bind. Below is the code from the class containing the collection, as well as snippets from the main WPF window xaml and code behind. Any clarifications or feedback would be greatly appreciated.
public class DOGMethodsRepo : INotifyPropertyChanged { ObservableCollection<Bulldog> bulldogs = new ObservableCollection<Bulldog>(); public ObservableCollection<Bulldog> Bulldogs { get { return bulldogs; } set { bulldogs = value; OnPropertyChanged("Bulldogs"); } } public Thread updateDataSets; public string activeDB; public ObservableCollection<Bulldog> ReturnBulldogs(string database) { using(var context = new DOGContext(database)) { var dogs = new ObservableCollection<Bulldog>(context.Bulldogs.ToList()); return dogs; } } public void DBUpdate() { while(true) { Bulldogs = ReturnBulldogs(activeDB); Thread.Sleep(5000); } } public void StartDBUpdate(string database) { activeDB = database; updateDataSets = new Thread(new ThreadStart(DBUpdate)); updateDataSets.Start(); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }
xmlns:inf="http://schemas.infragistics.com/xaml/wpf" ... <inf:XamDataGrid Name="xamDataGrid" Grid.Row="1" DataSource="{Binding}"> </inf:XamDataGrid>
DOGMethodsRepo repo = new DOGMethodsRepo(); xamDataGrid.DataContext = repo; xamDataGrid.DataSource = repo.Bulldogs; ... repo.StartDBUpdate(SelectedDatabase);
Hello Mike,
I believe that there is a conflict with the code that you are using in that the setting of the DataSource of your XamDataGrid in C# is likely conflicting with your binding here. I cannot say for sure which one is “winning” in this case as I’m unsure where your C# code is located, but it is possible that your binding could be failing as if you are just using “{Binding}” you will be binding to the DataContext, which appears to be your DOGMethodsRepo. I would recommend trying to modify the binding to be “{Binding Bulldogs}” instead to see if that helps.
Please let me know if you have any other questions or concerns on this matter.
Thanks for the response. Unfortunately, this didn't result in any change. The only way I can get the grid to update is by manually setting the datasource to Bulldogs using a dispatchertimer. I've tried dozens of different binds and data contexts in the code and xaml and just end up with nothing everytime.
Thank you for your update on this matter.
I am unsure why this would not work for you, as the Bulldogs collection is an ObservableCollection and so raises INotifyCollectionChanged updates that the grid will react to. One thing that is raising a bit of a red flag is the Thread updateDataSets in the original code you provided, which leads me to wonder if perhaps the grid is getting the initial Bulldogs collection via the binding, but perhaps is not receiving the updates due to them potentially happening on another thread?
Hey Andrew,That was a good idea, I hadn't considered that updating on a separate thread would be a problem. Since the collection is never cleared, I assumed the UI Thread would read it and update eventually, regardless of which Thread it's on. In response to your idea, I tried moving the collection update to the DispatcherTimer in the xaml class, hoping that it being on the UI Thread would work. Alas, no dice, still blank. The only thing I've gotten to work is manually resetting the Data Source every 5 seconds on the DispatcherTimer to the collection. Which is not ideal since that causes the datagrid to scroll to the top every update.
It is expected that the reset of the Data Source will reset the scroll as all of the records will be recreated by default. You may be able to get around this by using the different options of the DataSourceResetBehavior property of the grid. I believe that the best option in this case would be for the ObservableCollection to be updated on the UI thread if possible though, as this will raise the INotifyCollectionChanged notification on that thread and update the grid accordingly.