I am a beginner in WPF. It is necessary that the TextBox changes be dynamically displayed in a local variable. There is a variable in the main window:

private int year; 

And there is a TextBox:

 <TextBox Name="tb" TextWrapping="Wrap" Text="2015" VerticalAlignment="Top" /> 

How to display the value of tb in TextBox I understood. But now how to display the TextBox value in tb ?

The second question is where to do the bindings: in XAML or in code. Those. Which method more closely matches the MVVM pattern?

    1 answer 1

    Bindings need to be done in XAML.
    Initially, you need to bind the ViewModel to the DataContext of your window.
    Example:

     using System.Windows; using WpfApplication.View; using WpfApplication.ViewModel; namespace WpfApplication { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { public App() { var mw = new MainWindowView { DataContext = new MainViewModel() }; mw.Show(); } } } 

    MainWindowView is your window. MainViewModel - a class that implements the connection of the window with the model. In MainViewModel you create an instance of your model MyModel model = new MyModel() , and in the class MyModel you have your year variable, it is necessary that it be accessible, for example, through the public int Year {get { return year;}...} property. Then in the XAML code you need to bind via Binding :

     <TextBox Name="tb" TextWrapping="Wrap" Text="{Binding Path=Model.Year}" VerticalAlignment="Top" /> 

    Here is a very good article about MVVM for beginners: http://svyatoslavpankratov.blogspot.ru/2011/11/mvvm-pattern-1.html