Studying the pattern, I learned how to more less associate window elements with ViewModel properties, that is, classes, as I understand it, as intermediaries between code / logic and a window. However, when looking for how code / logic to interact with the ViewModel failed. When independent attempts at binding came to the same thing - there is no single control center. It seems the whole point is that there is no single center, but I absolutely do not understand how to implement such a logic. I tried to look for examples, but they all manipulate some prohibitively complex for a beginner C # features. Please explain how I, for example, make an authorization window and go to some kind of hall / homepage.

This will be the code zaml:

<StackPanel> <TextBox FontSize="20" Text="{Binding Login, Mode=TwoWay}"/> <TextBox FontSize="20" Text="{Binding Pass, Mode=TwoWay}"/> <Button Height="30" Click="Button_Click"/> </StackPanel> 

With # window code:

 public partial class MainWindow : Window { ViewModel ViewModel = new ViewModel(); public MainWindow() { InitializeComponent(); DataContext = ViewModel; } private void Button_Click(object sender, RoutedEventArgs e) { ViewModel.Login = "1111"; ViewModel.Pass = "2222"; } } 

ViewModel Code:

 class ViewModel : INotifyPropertyChanged { private string login; private string pass; public string Login { get { return login; } set { login = value; OnPropertyChanged("Login"); } } public string Pass { get { return pass; } set { pass = value; OnPropertyChanged("Pass"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 

I hope you do not go blind

As you can see, the only way to bind the ViewModel I own is to specify it in code using a DataContext. At the same time, an object is created which is not monitored anywhere. Not to mention the fact that to organize the interaction of all data among themselves. In this example, the logic is placed in the click method, since I cannot imagine how the logic written in a separate file can be linked here without crutches.

  • Look here , there seems to be a good example. - VladD
  • one
    In particular, the activation of changes in the VM must be through the command. - VladD
  • one
    If you have made the binding {Binding Login, Mode=TwoWay} , then this is ViewModel.Login = "1111"; in kodbihind do not need. Download and see an example . - Bulson
  • @Bulson, your example demonstrates the pattern very well. I do not have enough knowledge in terms of C # so that I can use the techniques from the example. But the mechanism of interaction is now clear to me. Thank you - lalala lala
  • I recommend that you copy the file of the RelayCommand class from this example RelayCommand It will be useful to you in the implementation of commands in your own projects. - Bulson

1 answer 1

See it.

That's what's wrong with you - you create VM classes in the window constructor. This is wrong, because in doing so you will not be able to easily link the VM classes to each other.

If you need to make several windows, you need to put the program logic in a common place. For example, in App.xaml.cs. In this case, you can easily link your windows. Example:

 public partial class App : Application { AuthorizationVM authVM = new AuthorizationVM(); MainVM mainVM; protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var authWindow = new AuthorizationWindow() { DataContext = authVM }; authWindow.Closed += OnAuthorizationFinished; authWindow.Show(); } void OnAuthorizationFinished(object sender, System.EventArgs e) { if (!authVM.IsAuthorizationSuccessful) Shutdown(); mainVM = new MainVM(authoVM.Credentials); var mainWindow = new MainWindow() { DataContext = mainVM }; mainWindow.Closed += OnMainWindowClosed; mainWindow.Show(); } void OnMainWindowClosed(object sender, System.EventArgs e) { Shutdown(); } } 

and so on.

Do not forget to specify in App.xaml ShutdownMode="OnExplicitShutdown" .