Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You shouldn't be putting a UserControl (like Grid) in your view model; that's the beauty of the View. Based on your example, you probably want a control like the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.aspx" rel="nofollow">DataGrid</a>, which displays things in an Excel-like table. Consider the following:</p> <pre><code>public class Model { public string Name { get; set; } public string Description { get; set; } public Categories Category { get; set; } public Grid PresenterContent { get; set; } } public class ViewModel : ViewModelBase { private ObservableCollection&lt;Model&gt; _model; public ObservableCollection&lt;Model&gt; Model { get { return _model; } set { if (_model != value) { _model = value; RaisePropertyChanged(() =&gt; Model); } } } public ViewModel() { Model = new ObservableCollection&lt;Model&gt;(); } } </code></pre> <p>This is what is in the code-behind (may not be <em>exactly</em> like this):</p> <pre><code>public partial class UserControl1 : public UserControl { UserControl1( ) { this.DataContext = new ViewModel( ); } } </code></pre> <p>And the Xaml would look like this:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding Model}" /&gt; </code></pre> <p>The idea is that your ViewModel prepares the data for presentation, and the entire presentation is defined in the Xaml (View). Using the <code>"{Binding xxx}</code>" you may access properties on the object that is the <code>'DataContext'</code> of the current control. So the Xaml defines the children of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspx" rel="nofollow">UserControl</a>, and every UserControl has a <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontext.aspx" rel="nofollow">DataContext</a> to which objects on the UI (Xaml) can be bound. So you can bind your DataGrid's <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource.aspx" rel="nofollow">ItemsSource</a> (the source of the items that populate the grid) to the DataContext's Model property by saying <code>ItemsSource="{Binding Model}"</code></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