如何在 WPF Datagrid 上启用滚动条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:11:25  来源:igfitidea点击:

how can I enable scrollbars on the WPF Datagrid?

wpfdatagridscroll

提问by Edward Tanguay

When I run the following Northwind WPF Toolkit Datagridcode from this article, I get a datagrid, but there are no scrollbarsand hence the user can only see part of the datagrid. I am using the newest version March 2009.

当我运行下面的罗斯文WPF工具包Datagrid的从代码这篇文章中,我得到一个数据网格,但有没有滚动条,因此用户只能看到数据网格的一部分。我使用的是 2009 年 3 月的最新版本。

What do I need to specify so that the WPF Datagrid has scrollbars?

我需要指定什么才能使 WPF Datagrid 具有滚动条?

I tried putting the datagrid in a ScrollViewer but that didn't help.

我尝试将数据网格放在 ScrollViewer 中,但这没有帮助。

XAML:

XAML:

<Window x:Class="TestDataGrid566.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="600" Width="800">
    <StackPanel>
        <toolkit:DataGrid x:Name="TheDataGrid" AutoGenerateColumns="True"/>
    </StackPanel>
</Window>

code-behind:

代码隐藏:

using System.Linq;
using System.Windows;
using TestDataGrid566.Model;

namespace TestDataGrid566
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            NorthwindDataContext db = new NorthwindDataContext();
            var customers = from c in db.Customers
                            select c;
            TheDataGrid.ItemsSource = customers;
        }
    }
}

回答by Kent Boogaart

Put the DataGridin a Grid, DockPanel, ContentControlor directly in the Window. A vertically-oriented StackPanelwill give its children whatever vertical space they ask for - even if that means it is rendered out of view.

DataGridGridDockPanelContentControl在或者直接Window。垂直方向StackPanel将为其子项提供他们要求的任何垂直空间 - 即使这意味着它被渲染出来。

回答by Peter Darvas

WPF4

WPF4

<DataGrid AutoGenerateColumns="True" Grid.Column="0" Grid.Row="0"
      ScrollViewer.CanContentScroll="True" 
      ScrollViewer.VerticalScrollBarVisibility="Auto"
      ScrollViewer.HorizontalScrollBarVisibility="Auto">
</DataGrid>

with : <ColumnDefinition Width="350" />& <RowDefinition Height="300" />works fine.

与 : <ColumnDefinition Width="350" />&<RowDefinition Height="300" />工作正常。

Scrollbars don't show with <ColumnDefinition Width="Auto" />& <RowDefinition Height="300" />.

滚动条不显示<ColumnDefinition Width="Auto" />& <RowDefinition Height="300" />

Also works fine with: <ColumnDefinition Width="*" />& <RowDefinition Height="300" />in the case where this is nested within an outer <Grid>.

也适用于:<ColumnDefinition Width="*" />&<RowDefinition Height="300" />在它嵌套在外部<Grid>.

回答by Jay

If any of the parent containers RowDefinitionHeight set to "Auto"also stoppers for scrollbars

如果任何父容器RowDefinition高度"Auto"也设置为滚动条的塞子

Alternatively you may set Height "*"

或者,您可以设置高度“*”

Which happened in my case.

这在我的情况下发生了。

回答by Alex Albu

Adding MaxHeightand VerticalScrollBarVisibility="Auto"on the DataGridsolved my problem.

添加MaxHeightVerticalScrollBarVisibility="Auto"DataGrid解决我的问题。

回答by Bianca

Add grid with defined height and width for columns and rows. Then add ScrollViewerand inside it add the dataGrid.

为列和行添加具有定义高度和宽度的网格。然后添加ScrollViewer并在其中添加 dataGrid。

回答by Lucas

In my case I had to set MaxHeightand replace IsEnabled="False"by IsReadOnly="True"

在我而言,我不得不设置MaxHeight和更换IsEnabled="False"IsReadOnly="True"