A_Binary_Story
A_Binary_Story

Reputation: 95

MVVM - WPF How do i bind my View to my Viewmodel?

I have my View called "FahrgemeinschaftenView.xaml" & my ViewModel "Fahrgemeinschaften.cs." So by default the View is bound to its own cs, which in this case would be "FahrgemeinschaftenView.xaml.cs". Im very new to MVVM, but it says the ModelViewlayer should contain all the logic. So i try to connect "FahrgemeinschaftenView.xaml" with "FahrgemeinschaftenViewModel.cs", but wasn`t able to figure out how do to that (Passing Data).

MVVM View to ViewModel Problem

Upvotes: 1

Views: 2228

Answers (1)

David Weis
David Weis

Reputation: 36

Your view (in this case FahrgemeinschaftenView) has a property named DataContext. The simplest way of binding your view to your ViewModel is setting an instance of FahrgemeinschaftenViewModel to the DataContext property on your view.

You can do this in your constructor in FahrgemeinschaftenView.xaml.cs. Just write:

DataContext = new FahrgemeinschaftenViewModel();

right under the call to InitializeComponent()

public FahrgemeinschaftenView()
{
   InitializeComponent();
   DataContext = new FahrgemeinschaftenViewModel();
}

Upvotes: 1

Related Questions