WPF DataGrid中的Scrollbars无法正常工作

时间:2017-01-12 09:46:47

标签: c# wpf

美好的一天,

我有一个WPF DataGrid,显示了一个大型DataTable的内容。此表比屏幕大得多,因此用户与此表交互需要滚动条才能查看所有列和行。 正如您可以看到图片1,垂直滚动条可见但已禁用,而水平滚动条根本看不到。

如何让滚动条工作?

在一些用户交互之后,DataGrid通过数据绑定到DataTable来填充:

this.topPhrases.DataContext = loadedValues.DefaultView;

其中topPhrases是DataGrind,laodedValues是DataTable

这是DataGrid的xaml代码:

    <DataGrid Name="topPhrases" Grid.Row="1"  Margin="10,0"  VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding}">
    </DataGrid>

GUI元素的层次结构是: 窗口 - &GT;网格状&GT; WrapPanel-&GT; ContentControl-&GT;网格状&GT;数据网格

我尝试过很多在互联网上找到的东西: how can I enable scrollbars on the WPF Datagrid? 但到目前为止没有任何建议。

1 个答案:

答案 0 :(得分:0)

  

将固定高度设置为RowDefinition使水平和垂直滚动条都起作用。但我真正想要的是DataGrid没有固定大小但是主窗口的大小(减去当然顶部工具栏的高度)

将第一个RowDefinition的高度设置为Auto,将第二个RowDefinition的高度设置为*,例如:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ToolBar />
        <DataGrid Grid.Row="1" />
    </Grid>
</Window>

另外请确保您不使用任何StackPanel。有关更多信息,请参阅我的回答:

Horizontal scroll for stackpanel doesn't work

如果您需要任何进一步的帮助,我建议您发布窗口的整个XAML标记。