Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Remove the DataContext binding since you are not explicitly doing MVVM pattern. No point of doing the binding.</p> <p><strong>In your Window.xaml</strong></p> <pre><code>&lt;Window&gt; &lt;Grid&gt; &lt;TextBlock Text="Id:"/&gt; &lt;TextBox Name="Id" Text="{Binding Path=Id}"/&gt; &lt;TextBlock Text="Login:"/&gt; &lt;TextBox Name="Login" Text="{Binding Path=Login}"/&gt; &lt;TextBlock Text="Name:"/&gt; &lt;TextBox Name="Name" Text="{Binding Path=Name}"/&gt; &lt;/Grid&gt; </code></pre> <p> Add this to your Behind the code <strong>Window.xaml.cs</strong></p> <pre><code> public class UserForm : Window { public Users userObject { get; set; } public UserForm(Users user) { InitializeComponent(); this.DataContext = user; } } </code></pre> <p>If you would do it in MVVM you would have done something like this</p> <p><strong>ViewModel.cs</strong></p> <pre><code> public class UserViewModel { private Users _model; public UserViewModel(Users model) { _model = model; } public int Id { get { return _model.Id; } } public string Login { get { return _model.Login; } set { _model.Login; } } public string Name { get { return _model.Name; } set { _model.Name; } } } </code></pre> <p>Now the ViewModel can be customize depending on what you need, you can expose the Model, inject it to the constructor or just set the property value if it is exposed. Don't forget to implement INotifyPropertyChanged if you want to propagate any values in the ViewModel back to the user interface.</p> <p><strong>View.xaml.cs</strong></p> <p>public class UserForm : Window {</p> <pre><code> public UserForm(Users user) { InitializeComponent(); this.DataContext = new UserViewModel(user); } </code></pre> <p><strong>View.xaml</strong></p> <p>You have two choices, you can explicitly set the DataContext just like what I did behind the code or you can create a public property that returns the UserViewModel. It's just the same</p> <p><strong>Model.cs</strong></p> <pre><code>public class Users { //Whatever properties you need } </code></pre> <p>Now this is a very simplistic example of MVVM pattern. Once you know the basics, you can then integrate some helpful frameworks that implements MVVM for you like <a href="http://caliburnmicro.codeplex.com/" rel="nofollow">Caliburn Micro</a> and <a href="http://mvvmlight.codeplex.com/" rel="nofollow">MVVM Toolkit</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload