Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, move as much as possible of the different settings out into reusable <code>Styles</code> and <code>DataTemplates</code>, leaving very little in the DataGrid itself:</p> <pre><code>&lt;UserControl ... &gt; &lt;UserControl.Resources&gt; &lt;Style x:Key="GridHeaderStyle" TargetType="{x:Type DataGridColumnHeader}"&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;Setter Property="FontSize" Value="12" /&gt; &lt;/Style&gt; &lt;Style x:Key="ReadOnlyGridStyle" TargetType="{x:Type DataGrid}" &gt; &lt;Setter Property="AutoGenerateColumns" Value="False" /&gt; &lt;Setter Property="IsReadOnly" Value="True" /&gt; &lt;Setter Property="GridLinesVisibility" Value="None" /&gt; &lt;Setter Property="CanUserAddRows" Value="False" /&gt; &lt;Setter Property="CanUserDeleteRows" Value="False" /&gt; &lt;Setter Property="CanUserResizeColumns" Value="False" /&gt; &lt;Setter Property="CanUserResizeRows" Value="False" /&gt; &lt;Setter Property="CanUserReorderColumns" Value="False" /&gt; &lt;Setter Property="ColumnHeaderStyle" Value="{StaticResource GridHeaderStyle}" /&gt; &lt;/Style&gt; &lt;DataTemplate x:Key="IngredientsCellTemplate" &gt; &lt;DataGrid ItemsSource="{Binding Ingredients}" Style="{StaticResource ReadOnlyGridStyle}" &gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Ingredients" Width="*" FontSize="12" Binding="{Binding Path=IngredientName}" /&gt; &lt;DataGridTextColumn Header="Quantite" Width="*" FontSize="12" Binding="{Binding Path=Qty}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;!-- A DataGrid using our Styles: --&gt; &lt;DataGrid ItemsSource="{Binding View}" Style="{StaticResource ReadOnlyGridStyle}" &gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Type" Width="200" FontSize="12" Binding="{Binding Path=Name}" /&gt; &lt;DataGridTemplateColumn Header="Ingredients" Width="*" CellTemplate="{StaticResource IngredientsCellTemplate}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/UserControl&gt; </code></pre> <p>Then it gets a lot easier to create new DataGrids in your code-behind, using the existing Styles:</p> <pre><code>var datagrid = new DataGrid(); datagrid.Style = FindResource("ReadOnlyGridStyle") as Style; datagrid.Columns.Add(new DataGridTextColumn() { Header = "Type", Width = new DataGridLength(200), FontSize = 12, Binding = new Binding("Name") }); datagrid.Columns.Add(new DataGridTemplateColumn() { Header = "Ingredients", Width = new DataGridLength(1, DataGridLengthUnitType.Star), CellTemplate = FindResource("IngredientsCellTemplate") as DataTemplate }); datagrid.ItemsSource = ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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