0
votes

I have got a WPF Application, which contains a Window, inside it there is a DataGrid. On Startup, the DataGrid is completely empty, its Columns are created at runtime by code.

I'm binding to a DataTable. The first coloumn is a single Text Field. I need to show one more Cell per Row, but as a Stackpanel, which itself holds some UserControls.

Actually I'm trying to first insert a blank column as placeholder for my StackPanel and later insert a StackPanel from Code to each Cell.

Unfortunately i don't get it running for some reasons. I cannot put a new Item to the Cell. Can anybody help me, please?

Best Regards,

Jonas

2
Please explain more, give us your code - Mohammed A. Fadil

2 Answers

1
votes

Try and use a DataGridTemplateColumn, then you can put a stackpanel in each cell

1
votes

I would recommend to design and fill the datagrid via XAML.

  • YourItems is the Collection of object which you want to show
  • userControlColumn is the additional column for your usercontrol
  • firstColumn is your first DataGridTextColumn

    <DataGrid x:Name="myGrid" ItemsSource="{Binding YourItems}">
        <DataGrid.Columns>
            <DataGridTemplateColumn x:Name="userControlColumn" Header="Column1">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <ns:YourCustomControl/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="firstColumn" Header="Columns2" Binding="{Binding YourDesiredProperty}"/>
        </DataGrid.Columns>
    </DataGrid>