Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The sequence is following:</p> <p>1) Move code from the MainWindow class to a custom class and set the <code>DataContext</code> property:</p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); } } public class MainWindowViewModel { public MainWindowViewModel() { myData = new List&lt;Item&gt; { new Item{ Id=5, Description="Brown Car", Title="my car"}, new Item{Id=1,Description="sweet dog", Title="my dog"}, }; } public List&lt;Item&gt; MyData { get { return myData; } } List&lt;Item&gt; myData; } </code></pre> <p>2) Change the binding of the <code>DataGrid</code>:</p> <pre><code>ItemsSource="{Binding MyData}" </code></pre> <p>3) Add new ResourceDictionary with the name <code>DataGridSample.xaml</code>, open its properties (Right Click -> Properties), set the <code>BuildAction</code> property to the DesignData and clear the value of the <code>CustomTool</code> property.</p> <p>4) Copy this code, but change the namespace <code>vm</code> from the <code>WpfApplication1</code> to your one:</p> <pre><code>&lt;vm:MainWindowViewModel xmlns:vm="clr-namespace:WpfApplication1"&gt; &lt;vm:MainWindowViewModel.MyData&gt; &lt;vm:Item Id="1" Title="My dog" Description="Sweet dog" /&gt; &lt;vm:Item Id="5" Title="My car" Description="Brown car" /&gt; &lt;/vm:MainWindowViewModel.MyData&gt; &lt;/vm:MainWindowViewModel&gt; </code></pre> <p>5) Return to the <code>MainWindow.xaml</code> and add next lines to the <code>Window</code> element where all the declarations are:</p> <pre><code>xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DataContext="{d:DesignData Source=DataGridSample.xaml}" </code></pre> <p>After that you can switch to the design mode and you will see the datagrid with two lines.</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