数据网格上缺少滚动条

时间:2014-06-24 14:16:34

标签: c# wpf datagrid

我想在datagrid上使用滚动条。请看下面的代码:

<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时,我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以在ScrollBar添加DataGrid而无需添加ScrollViewer。您可以使用ScrollViewer.HorizontalScrollBarVisibilityScrollViewer.VerticalScrollBarVisibility附加属性显示ScrollBar

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

答案 1 :(得分:0)

DataGrid的许多功能都可以自动化,例如滚动。为此,您可以使用 Horizo​​ntalScrollBarVisibility VerticalScrollBarVisibility

<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>