阻止内容滚动

时间:2017-10-20 08:18:38

标签: wpf

基本上我是一个带有多个标题的DataGrid,在wpf中没有实现这一点的控件,所以我已经实现了自己的解决方案。

控件看起来像这样:

[Header1          ][Header2][Header3][Header4         ]
[column 1][column2][Column3][Column4][Column5][Column6]

因此,您可以在特定列下看到我有一个分组x列的标题。 这是我的实施:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" Width="{Binding Path=Width, ElementName=RankingsHalfTime}">
        <!-- multiple headers -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn0, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn1, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn2, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn3, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn4, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn5, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn6, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn7, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn8, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn9, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn10, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn11, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn12, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn13, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn14, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn15, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn16, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn17, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn18, Path=ActualWidth}"/>
            <ColumnDefinition Width="{Binding ElementName=HalfColumn19, Path=ActualWidth}"/>
        </Grid.ColumnDefinitions>

        <!-- Multiple Headers-->
        <Border Grid.Column="2" Grid.ColumnSpan="6" BorderThickness="1 1 1 1" Margin="-1 0 -1 0">
            <Label TextBlock.FontWeight='Bold' Background="WhiteSmoke" Content="Header 1" HorizontalAlignment="Center"/>
        </Border>
        <Border Grid.Column="8" Grid.ColumnSpan="6" BorderThickness="1 1 1 1" Margin="0 0 -2 0">
            <Label TextBlock.FontWeight='Bold' Background="WhiteSmoke" HorizontalAlignment="Center">Header 2</Label>
        </Border>

    </Grid>
    <DataGrid AutoGenerateColumns="False" 
                    CanUserAddRows="false" Grid.Column="1" Grid.Row="1" 
                    RowStyle="{StaticResource CustomRowStyle}"
                    ItemsSource="{Binding RankingController.RankingsHalfTime}" 
                    IsReadOnly="True" x:Name="RankingsHalfTime" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="0" x:Name="HalfColumn0" Binding="{Binding Position}"/>
            <DataGridTextColumn Header="1" x:Name="HalfColumn1" Binding="{Binding Team}" Width="200" />

            <DataGridTextColumn Header="2" x:Name="HalfColumn2" />
            <DataGridTextColumn Header="3" x:Name="HalfColumn3" />
            <DataGridTextColumn Header="4" x:Name="HalfColumn4" />
            <DataGridTextColumn Header="5" x:Name="HalfColumn5" />
            <DataGridTextColumn Header="6" x:Name="HalfColumn6" />
            <DataGridTextColumn Header="7" x:Name="HalfColumn7" />

            <DataGridTextColumn Header="8" x:Name="HalfColumn8"/>
            <DataGridTextColumn Header="9" x:Name="HalfColumn9" />
            <DataGridTextColumn Header="10" x:Name="HalfColumn10" />
            <DataGridTextColumn Header="12" x:Name="HalfColumn11" />
            <DataGridTextColumn Header="12" x:Name="HalfColumn12" />
            <DataGridTextColumn Header="13" x:Name="HalfColumn13" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

代码工作得很好,我想要的结果是成功实现的,但是有一个问题:

当窗口调整大小时,DataGrid会显示一个水平滚动条,这会破坏多个标题的逻辑,实际上,当用户移动水平滚动条时,多个标题不会固定到原始位置,所以最终结果就是这个(当我移动滚动条时):

           [Header1          ][Header2][Header3][Header4         ]
[column 1][column2][Column3][Column4][Column5][Column6]
[scrollbar                                                       ]

我正在寻找一种方法来防止多个页眉滚动并让它们卡在原始位置。您可以尝试我的代码以获得更好的练习解释。

0 个答案:

没有答案