1

I want to add 2-3 buttons to the rows in the last column of my datagrid using the backend C# and not XAML. I managed to add one button to the cells but I'm having trouble adding anymore past that.

I have tried creating a new FrameworkElementFactory and adding it into the column but it just replaces the previous button instead of adding the button.

        DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
        buttonColumn.Header = "Actions";
        buttonColumn.Width = 209;

        DataTemplate buttonTemplate = new DataTemplate();
        FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
        buttonTemplate.VisualTree = buttonFactory;

        buttonFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
        buttonFactory.SetValue(ContentProperty, "A");
        buttonColumn.CellTemplate = buttonTemplate;

        dGrid_SavedData.Columns.Add(buttonColumn);

3 Answers 3

1

Regardless of whether you create it programmatically or in XAML, a DataTemplate can only have single root element so you should set the VisualTree property to a FrameworkElementFactory for a Panel and use the AppendChild method to add the button factories to the panel factory, e.g.:

DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
buttonColumn.Header = "Actions";
buttonColumn.Width = 209;

DataTemplate buttonTemplate = new DataTemplate();
FrameworkElementFactory panelFactory = new FrameworkElementFactory(typeof(StackPanel));
buttonTemplate.VisualTree = panelFactory;

FrameworkElementFactory buttonAFactory = new FrameworkElementFactory(typeof(Button));
buttonAFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
buttonAFactory.SetValue(ContentProperty, "A");

FrameworkElementFactory buttonBFactory = new FrameworkElementFactory(typeof(Button));
buttonBFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
buttonBFactory.SetValue(ContentProperty, "B");

panelFactory.AppendChild(buttonAFactory);
panelFactory.AppendChild(buttonBFactory);

buttonColumn.CellTemplate = buttonTemplate;

dGrid_SavedData.Columns.Add(buttonColumn);
Sign up to request clarification or add additional context in comments.

Comments

0

So, I’m on an iPad, so I can’t give much detail. First let me say that this is a very hard way to do this and I recommend just about anything else. But if you absolutely have to fully dynamically generate your grid, you need to put the button in a container. For example, a vertical stack panel. Then you can add as many buttons as you want. Good luck!

Comments

0

Here is the Code Snippet

<DataGrid x:Name="dgStudent" d:ItemsSource="{d:SampleData ItemCount=5}" Margin="0,44,0,0" Grid.RowSpan="2">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Edit">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Name="btnEdit" Content="Edit" Click="btnEdit_Click" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Delete">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Name="btnDelete" Content="Delete" Click="btnDelete_Click" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.