Lewis Heslop
Lewis Heslop

Reputation: 590

Databinding causing other functions on page not to work when filling datagrid

I have a datagrid in my View. The datagrid's itemssource property is bound as such

ItemsSource="{Binding}"

Addtionally in the codebehind I have set datacontext by doing the following:

DataContext = ProcedureDatabaseViewModel.Procedures();

The Procedures function in the viewmodel successfully outputs a list which the DataGrid successfully displays.

The issue now is that the datacontext of the entire page is now set to the above. The result of this is that other elements that interect with the VM no longer work. I.E. buttons with commands that are found in the VM. I have tried removing the setting of the datacontext in the code behind but cannot figure out how to populate the datagrid otherwise. Please note that when the DataContext is not set in the code behind the context is changed to the VM, I believe, and thus, the other elements begin working again. I have tried changing the Itemssource property to target the list of objects that I wish to populate the datagrid with but it hasnt worked.

The list is

List<procedure> Procedures

and it is in the ProdureDatabaseViewModel. I tried to target it as

ItemsSource="{Binding ProdureDatabaseViewModel.Procedures}"

But this has not worked either.

Can someone please advise me on the correct way to do this?

Upvotes: 0

Views: 53

Answers (3)

AnjumSKhan
AnjumSKhan

Reputation: 9827

I have tried removing the setting of the datacontext in the code behind but cannot figure out how to populate the datagrid otherwise.

You can set the DataContext of your DataGrid separately like so, MyDataGrid.DataContext = ProcedureDatabaseViewModel.Procedures();.

And apply separate DataContext for your Page.

Upvotes: 0

Markus Dietrich
Markus Dietrich

Reputation: 618

The cleanest way is to use ItemsSource to bind your Procedures collection to your DataGrid. For this to work, Procedures has to be a Property. To avoid further issues, use an ObservableCollection. It should look like this:

ObservableCollection<procedure> Procedures { get; set; }

Then you should be able to simply bind it via

ItemsSource="{Binding Procedures}"

Upvotes: 1

Bexo
Bexo

Reputation: 198

I prefer to set the DataContext in the constructor of the view to the complete ViewModel (which I created for this special view).

So I do something like this in the constructor of the view:

public View(ProcedureDatabaseViewModel viewModel)
{
    this.DataContext = viewModel;
}

This way everything else should still work and you can use more than just the procedures.

Next you bind the procedures to your datagrid:

ItemsSource="{Binding ProcedureList}"

Do note that for this to work, "Procedures" needs to be a property. It's not clear in your question if it is a function, a property or just a simple class member. If it is a function, you can do it like this in your view model:

public List<procedure> ProcedureList
{
    get { return this.Procedures(); }
}

Upvotes: 0

Related Questions