Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically create a datagrid in WPF?
    primarykey
    data
    text
    <p>I have the following datagrid in XAML:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" &gt; &lt;DataGrid.ColumnHeaderStyle&gt; &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;Setter Property="FontSize" Value="12" /&gt; &lt;/Style&gt; &lt;/DataGrid.ColumnHeaderStyle&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Type" Width="200" FontSize="12" Binding="{Binding Path=Name}" /&gt; &lt;DataGridTemplateColumn Header="Ingredients" Width="*"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;DataGrid ItemsSource="{Binding Ingredients}" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" &gt; &lt;DataGrid.ColumnHeaderStyle&gt; &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;Setter Property="FontSize" Value="12" /&gt; &lt;/Style&gt; &lt;/DataGrid.ColumnHeaderStyle&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;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>I am trying to find a way to create the datagrid dynamically (in-code) so that I can create multiple copies of it and bind it to different datasources at run-time. </p> <p>Is this possible? Anyone know how I could go about it for a datagrid complicated like this?</p>
    singulars
    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.
 

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