Help with a MVVM view

Asked

Viewed 63 times

0

I’m reading the that MVVM tutorial. But I’m not understanding the following:

<Window x:Class = "MVVMDemo.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:local = "clr-namespace:MVVMDemo"
   xmlns:views = "clr-namespace:MVVMDemo.Views"
   mc:Ignorable = "d"
   Title = "MainWindow" Height = "350" Width = "525">

   <Grid>
      <views:StudentView x:Name = "StudentViewControl" Loaded = "StudentViewControl_Loaded"/>
   </Grid>

</Window>

using System.Windows;

namespace MVVMDemo {

   /// <summary>
      /// Interaction logic for MainWindow.xaml
   /// </summary>

   public partial class MainWindow : Window {

      public MainWindow() {
         InitializeComponent();
      }

      private void StudentViewControl_Loaded(object sender, RoutedEventArgs e) {
         MVVMDemo.ViewModel.StudentViewModel studentViewModelObject = 
            new MVVMDemo.ViewModel.StudentViewModel();
         studentViewModelObject.LoadStudents();

         StudentViewControl.DataContext = studentViewModelObject;
      }
   }
}

Question:

When and where the class was created StudentViewControl who owns the property DataContext changed in the last line of the method StudentViewControl_Loaded ?

1 answer

2


If you look at the project used by the tutorial, you will see that there is a ViewModel by the name of StudentViewModel and a UserControl which is the View by the name of Studentview. Studentviewcontrol is just the name which he assigned to the viewthat he is using... In the tutorial is the Step 6

  • One, it would then be like a variable. Something like studentViewControl = new StudentView(). Is that it? .

  • 1

    It creates this ""instance of Studentviewcontrol when you use <views:StudentView x:Name = "StudentViewControl" Loaded = "StudentViewControl_Loaded"/> from there he can access StudentViewControl.DataContext

Browser other questions tagged

You are not signed in. Login or sign up in order to post.