Search code examples
c#wpfxamldatagriddatagridtemplatecolumn

how to place datagrid column at the end of the datagrid row in wpf?


I have a datagrid inside a grid with two cols one is text and other one is button and my wpf code below

<Grid Grid.Column="2"
          Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=GroupName}"
                                    Header="Group Name" />
                <DataGridTemplateColumn Header="Actions"
                                        Width="2"   
                                        ToolTipService.ToolTip="Remove Group"                                            
                                        IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button 
                                    Style="{StaticResource CustXButton}"
                                    Command="Delete" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

enter image description here

how to place the second datagrid column i.e 'x' button the end of the datagrid in that row ?I tried to set margin for button but that didnt work.Any help is greatly appreciated.Thanks


Solution

  • Just add Width="*" to your Group Name column to make it use all the available space.

        <Grid Grid.Column="2"
          Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=GroupName}"
                                    Header="Group Name" 
                                    Width="*"/>
                <DataGridTemplateColumn Header="Actions"
                                        Width="2"   
                                        ToolTipService.ToolTip="Remove Group"                                            
                                        IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button 
                                    Style="{StaticResource CustXButton}"
                                    Command="Delete" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>