We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load a button in caption summary row and prevent expand and collapse of groups in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: summaries

You can load a button inside the CaptionSummaryCell by customizing the ControlTemplate of the GridCaptionSummaryCell using Style in WPF DataGrid (SfDataGrid).

 

<Style TargetType="syncfusion:GridCaptionSummaryCell">
                <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="syncfusion:GridCaptionSummaryCell">
                        <Border x:Name="PART_GridCaptionSummaryCellBorder"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="60"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <StackPanel Grid.Column="0">
                                    <Button Content="Click" Width="50" Height="20" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type syncfusion:SfDataGrid}}, Path=DataContext.ClickCommand}" CommandParameter="{Binding}"/>
                                </StackPanel>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 

You can prevent the expanding and collapsing of the group upon clicking the button, by handling the SfDatagrid.GroupCollapsing and SfDatagrid.GroupExpanding events as shown in the below the code.

public MainWindow()
{
    InitializeComponent();
    this.datagrid.GroupCollapsing += Datagrid_GroupCollapsing;
    this.datagrid.GroupExpanding += Datagrid_GroupExpanding;
}
private void Datagrid_GroupExpanding(object sender, Syncfusion.UI.Xaml.Grid.GroupChangingEventArgs e)
{
    //Code to skip expanding the group when clicking the button in caption row. 
    if (Mouse.DirectlyOver is Button) 
    {
        e.Cancel = true;
    }
}
private void Datagrid_GroupCollapsing(object sender, Syncfusion.UI.Xaml.Grid.GroupChangingEventArgs e)
{
    //Code to skip collapsing the group when clicking the button in caption row. 
    if (Mouse.DirectlyOver is Button)
    {
        e.Cancel = true;
    }
}

 

The output upon executing the above task will be as shown below.

Load a button in summaryrow in WPF DataGrid

View sample in GitHub.

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile