5

我将 DataGrid 绑定到自定义对象的 ObservableCollection。间歇性地,我得到(可能在 2 或 3 行上)一个按钮,如 nubbie 对象(大约 4 像素宽和行的高度)出现在左边缘,并导致该行的列略微缩进,以便它们不会t 排队。我不认为这些行有什么特别之处,因为如果我将网格重新绑定到相同的对象,小块可能根本不会发生或可能出现在不同的行上。

我正在使用一些自定义样式,并且我的第一列是 DataTemplate,因此我将其包含在下面,以防任何人都可以看到其中可能导致我的问题的任何内容。在这一点上我有点难过...

<DataGrid  Style="{DynamicResource WPFDataGridStyle}" Background="White" BorderBrush="LightGray" FontSize="13"
        CanUserReorderColumns="True" HorizontalGridLinesBrush="#FFEFEFEF" VerticalGridLinesBrush="#FFEFEFEF" HeadersVisibility="Column" 
        AlternatingRowBackground="#FFF4F4F4" CanUserResizeRows="False" SelectionMode="Single" AutoGenerateColumns="False" 
        CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="True" FrozenColumnCount="3"
        ItemsSource="{Binding VM.FilteredSteamJobs,Mode=TwoWay,Source={StaticResource VM}}" 
        SelectedItem="{Binding VM.SelectedJob,Mode=TwoWay,Source={StaticResource VM}}"
        Visibility="{Binding VM.IsScheduleLoaded,Mode=OneWay,Source={StaticResource VM},Converter={StaticResource BoolToVisConv}}"  >
    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#AAA7CDF0"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#AAA7CDF0"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/>

        <Style x:Key="WPFDataGridStyle" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
            <Setter Property="ColumnHeaderStyle" Value="{DynamicResource ColumnHeaderStyle1}"/>
            <Setter Property="CellStyle" Value="{DynamicResource CellStyle1}"/>
        </Style>

        <Style x:Key="ColumnHeaderStyle1" TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
            <Setter Property="Height" Value="25"/>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFC8E0FF" Offset="1"/>
                        <GradientStop Color="#FFF5FAFF" Offset="0"/>
                        <GradientStop Color="#FFDBEBFF" Offset="0.5"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Padding" Value="3"/>
            <Setter Property="BorderThickness" Value="0.5,0"/>
            <Setter Property="BorderBrush" Value="DarkGray"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontSize" Value="13" />
        </Style>

        <Style x:Key="CellStyle1" TargetType="{x:Type DataGridCell}">
            <Setter Property="Height" Value="25"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Padding" Value="3,0"/>
        </Style>
    </DataGrid.Resources>

    <DataGrid.Columns>

        <!--Selection Checkbox Column-->
        <DataGridTemplateColumn IsReadOnly="True" CanUserResize="False" CanUserSort="False" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3"
                                  IsChecked="{Binding Path=IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                  Visibility="{Binding Status,Converter={StaticResource StatusToSelectVisConv}}">
                            <CheckBox.LayoutTransform>
                                <ScaleTransform ScaleX="1.2" ScaleY="1.2"/>
                            </CheckBox.LayoutTransform>
                        </CheckBox>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox VerticalAlignment="Center" Checked="HeaderSelect_Checked" Unchecked="HeaderSelect_Unchecked">
                            <CheckBox.LayoutTransform>
                                <ScaleTransform ScaleX="1.3" ScaleY="1.3"/>
                            </CheckBox.LayoutTransform>
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>                 
            </DataGridTemplateColumn.HeaderTemplate>
        </DataGridTemplateColumn>
4

1 回答 1

8

我也一直在努力解决这个问题,如果我没有弄错,你必须RowHeaderWidth="0"在 DataGrid 上设置以避免这个“错误”。

显然,某些随机行的行标题显示,即使HeadersVisibility设置为Column.

于 2012-11-14T17:24:02.190 回答