Oiad462
Oiad462

Reputation: 13

XAML - DataGrid Columns move when data is loaded to the rows

I have a DataGrid that loads it's cell information from an XML file. When the data is loaded to the grid, you can see that the cells move slightly to the right and a faint cell appears on the left. I would like to get that to stop.

I've played with all of the Layout options. I've tried removing the border. I didn't do much with the Columns section in Visual Studio.

The cels are all set to a specific Width except for the one on the far right. That cell is set to Auto and contains only a button defined in the XAML, not the XML.

<Window.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="TextBlock.TextAlignment" Value="Left"/>
    </Style>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
    </Style>
</Window.Resources>

                    <DataGrid x:Name="dgActions" Margin="10,93,20,10" AlternatingRowBackground="#FFB4B4B4" RowBackground="White" IsReadOnly="True" SelectionMode="Extended"
                          VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserResizeColumns="False" UseLayoutRounding="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" Binding="{Binding Element[name].Value}" Width="175"/>
                        <DataGridTextColumn Header="KB#" Binding="{Binding Element[knowledge].Value}" Width="50"/>
                        <DataGridTextColumn Header="Description" Binding="{Binding Element[description].Value}" Width="665"/>
                        <DataGridTemplateColumn Header="Send" Width="Auto">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button x:Name="btnSendScript" Content="Send" CommandParameter="{Binding}" Click="btnSendScript_Click" FlowDirection="LeftToRight" HorizontalAlignment="Right"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

Before the XML values are loaded

Here is what happens when the data loads...

When the XML values are loaded...

Upvotes: 0

Views: 581

Answers (1)

AnjumSKhan
AnjumSKhan

Reputation: 9827

That left space is for DataGridRowHeader. To remove it, use below Style.

       <DataGrid ...>
            <DataGrid.RowHeaderStyle>
                <Style TargetType="DataGridRowHeader">                    
                    <Setter Property="Width" Value="0"/>
                </Style>
            </DataGrid.RowHeaderStyle>
            ...
       </DataGrid>

Upvotes: 1

Related Questions