如何在 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
how can I enable scrollbars on the WPF Datagrid?
提问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 DataGrid
in a Grid
, DockPanel
, ContentControl
or directly in the Window
. A vertically-oriented StackPanel
will give its children whatever vertical space they ask for - even if that means it is rendered out of view.
把DataGrid
在Grid
,DockPanel
,ContentControl
在或者直接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 RowDefinition
Height set to "Auto"
also stoppers for scrollbars
如果任何父容器RowDefinition
高度"Auto"
也设置为滚动条的塞子
Alternatively you may set Height "*"
或者,您可以设置高度“*”
Which happened in my case.
这在我的情况下发生了。
回答by Alex Albu
Adding MaxHeight
and VerticalScrollBarVisibility="Auto"
on the DataGrid
solved my problem.
添加MaxHeight
和VerticalScrollBarVisibility="Auto"
对DataGrid
解决我的问题。
回答by Bianca
Add grid with defined height and width for columns and rows. Then add ScrollViewer
and inside it add the dataGrid.
为列和行添加具有定义高度和宽度的网格。然后添加ScrollViewer
并在其中添加 dataGrid。
回答by Lucas
In my case I had to set MaxHeight
and replace IsEnabled="False"
by IsReadOnly="True"
在我而言,我不得不设置MaxHeight
和更换IsEnabled="False"
由IsReadOnly="True"