WPF xaml滚动Datagrid内容并保持标题固定

hgncfbus  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(214)

我正在滚动整个Datagrid,包括标题,如下所示。然而,我无法找到一种方法来保持列标题旁边的滚动,让他们固定。如何到达?

<Grid Name="rowRecs0" Grid.Row="0">

            <ScrollViewer Height="200" VerticalScrollBarVisibility="Auto">

                <StackPanel>
                    <DataGrid
                        x:Name="datagridRecipes"
                        VerticalAlignment="Top"
                        AutoGenerateColumns="False"
                        CanUserAddRows="False"
                        CanUserReorderColumns="False"
                        CanUserResizeColumns="False"
                        CanUserResizeRows="False"
                        CanUserSortColumns="False"
                        IsReadOnly="True"
                        ItemsSource="{Binding UsersItems}"
                        SelectedIndex="{Binding UserSelectedIndex}"
                        SelectedItem="{Binding UserSelectedItem}"
                        SelectionChanged="datagridRecipes_SelectionChanged">

                        <DataGrid.Columns>

                            <materialDesign:DataGridTextColumn
                                Width="*"
                                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"
                                Header="NAME">
                                <materialDesign:DataGridTextColumn.ElementStyle>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="VerticalAlignment" Value="Center" />
                                        <Setter Property="Height" Value="30" />
                                    </Style>
                                </materialDesign:DataGridTextColumn.ElementStyle>

                                <materialDesign:DataGridTextColumn.Binding>
                                    <Binding Path="RecipeName" />
                                </materialDesign:DataGridTextColumn.Binding>
                            </materialDesign:DataGridTextColumn>

                        </DataGrid.Columns>
                    </DataGrid>

                </StackPanel>

            </ScrollViewer>
        </Grid>
k97glaaz

k97glaaz1#

DataGrid已经有了内置的行滚动功能,它不会影响列标题(也不会影响行标题)。你只需要从你的布局XAML中删除你的Grid,ScrollViewer和StackPanel。在那之后,一切都将按照它应该的那样工作。
已更正布局的XAML:

<!--<Grid Name="rowRecs0" Grid.Row="0">
<ScrollViewer Height="200" VerticalScrollBarVisibility="Auto">
<StackPanel>-->
    <DataGrid Height="200"
        x:Name="datagridRecipes"
        VerticalAlignment="Top"
        AutoGenerateColumns="False"
        CanUserAddRows="False"
        CanUserReorderColumns="False"
        CanUserResizeColumns="False"
        CanUserResizeRows="False"
        CanUserSortColumns="False"
        IsReadOnly="True"
        ItemsSource="{Binding UsersItems}"
        SelectedIndex="{Binding UserSelectedIndex}"
        SelectedItem="{Binding UserSelectedItem}"
        SelectionChanged="datagridRecipes_SelectionChanged">

        <DataGrid.Columns>
            <materialDesign:DataGridTextColumn
                Width="*"
                EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"
                Header="NAME">
                <materialDesign:DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Setter Property="Height" Value="30" />
                    </Style>
                </materialDesign:DataGridTextColumn.ElementStyle>

                <materialDesign:DataGridTextColumn.Binding>
                    <Binding Path="RecipeName" />
                </materialDesign:DataGridTextColumn.Binding>
            </materialDesign:DataGridTextColumn>

        </DataGrid.Columns>
    </DataGrid>

<!--</StackPanel>
</ScrollViewer>
</Grid>-->

相关问题