Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
15
Binding XamDataGrid to Data derived from Entity Framework
posted

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);