【问题标题】:XAML - DataGrid Columns move when data is loaded to the rowsXAML - DataGrid 列在数据加载到行时移动
【发布时间】:2017-04-13 03:57:54
【问题描述】:

我有一个从 XML 文件加载它的单元格信息的 DataGrid。当数据加载到网格中时,您可以看到单元格略微向右移动,并且左侧出现了一个微弱的单元格。我想让它停止。

我玩过所有的布局选项。我试过删除边框。我对 Visual Studio 中的 Columns 部分做的不多。

除了最右边的那个以外,所有的 cel 都设置为特定的宽度。该单元格设置为“自动”,并且仅包含在 XAML 中定义的按钮,而不是 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

这是加载数据时发生的情况...

When the XML values are loaded...

【问题讨论】:

  • 如果用于选择Rows,则剩余空间。

标签: wpf xaml


【解决方案1】:

剩下的空间是DataGridRowHeader。 要删除它,请使用下面的Style。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多