0

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

Mohammed A. Fadil
  • 9,205
  • 7
  • 50
  • 67
redflag237
  • 222
  • 4
  • 16

2 Answers2

2

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>
    
akjoshi
  • 15,374
  • 13
  • 103
  • 121
csteinmueller
  • 2,427
  • 1
  • 21
  • 32
1

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

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • 1
    Hi, How can I set this DataGridTemplateColumn by Code? I found this example, but i cannot get it running: http://stackoverflow.com/questions/1104164/wpf-datagridtemplatecolumn-am-i-missing-something – redflag237 Apr 25 '12 at 08:43
  • in your xaml specify the data template and give it a Name. then in you code you get the data template by using FindResource. Then set the CellTemplate of the DataGridTemplateColumn the result. – Dean Chalk Apr 25 '12 at 08:47
  • Yeah, DataGridTemplateColumn works fine, my mistake was, that i put the DataTemplate just inside the DataGrid, and FindRessource was unable to find it there. I moved it to the Windows.Ressources Tag and it works perfect. Another Problem i've got is, how can i access this even created StackPanel inside DataGrid by code and fill it with some Objekts? – redflag237 Apr 27 '12 at 07:24