数据网格上缺少滚动条

本文关键字:滚动条 数据网 网格 数据 | 更新日期: 2023-09-27 18:10:01

我想在数据网格上使用滚动条。看看下面的代码:

<UserControl x:Class="CustomCopyNas.UserControls.FolderControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300" Width="700">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <ScrollViewer>
            <DataGrid Grid.Row="0" ItemsSource="{Binding Folders, Mode=TwoWay}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Path">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox PreviewMouseDown="UIElement_OnPreviewMouseDown" Text="{Binding Path}" IsReadOnly="True"></TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>
    </Grid>
</UserControl>

我尝试将datagrid包装到ScrollViewer中,但这不起作用。我要做什么,当我想启用scrollViewer在我的数据网格,我怎么能做到这一点?

数据网格上缺少滚动条

您可以将ScrollBar s添加到DataGrid中,而无需添加ScrollViewer。您可以使用ScrollViewer.HorizontalScrollBarVisibilityScrollViewer.VerticalScrollBarVisibility附加属性使ScrollBar s出现:

<DataGrid ScrollViewer.HorizontalScrollBarVisibility="Visible"
    ScrollViewer.VerticalScrollBarVisibility="Visible" />

DataGrid的许多功能可以自动化,例如滚动。你可以使用HorizontalScrollBarVisibilityVerticalScrollBarVisibility

<UserControl x:Class="CustomCopyNas.UserControls.FolderControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300" Width="700">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
        <DataGrid HorizontalScrollBarVisibility="Auto"  VerticalScrollBarVisibility="Auto" Grid.Row="0" ItemsSource="{Binding Folders, Mode=TwoWay}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Path">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox PreviewMouseDown="UIElement_OnPreviewMouseDown" Text="{Binding Path}" IsReadOnly="True"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
</Grid>