WPF使用DataGrid的Scrollviewer而不是外部ScrollViewer

时间:2017-04-12 10:44:26

标签: c# wpf xaml datagrid scrollviewer

情境:
我有一个MainWindow。在这个MainWindow里面我有一个Grid。在网格内部我有一些按钮和其他东西。另外,我在Grid的末尾有一个带有ContentControl的ScrollViewer。我使用MVVM切换ContentControl的内容。我总是在ContentControl的内容中加载UserControl。 在这些UserControls的内部,我有一个包含DataGrid的Grid,其VerticalScrollBarVisibility设置为Auto。

<MainWindow>
    <ScrollViewer>
        <ContentControl />
    <ScrollViewer>
<MainWindow>

加载UserControl后,层次结构如下所示:

<MainWindow>
    <ScrollViewer>
        <ContentControl>
            <Grid>
                <DataGrid VerticalScrollBarVisibility="Auto">
            <Grid>
        <ContentControl>
    <ScrollViewer>
<MainWindow>

问题:
我希望DataGrid控制滚动,但因为在contentcontrol周围有一个scrollviewer,DataGrid周围的组件认为,它们具有无限高度,所以外部ScrollViewer控制滚动。

可能的解决方案:
Here我读到了:

  

你需要在内部ScrollViewer上设置一个高度,否则就是这样   根据内容的大小,尽可能多地拉伸。

在Code示例中,我无法在内部滚动条中看到任何内容:

<ScrollViewer Grid.Row="1">
    <DataGrid />
</ScrollViewer>

但我能够在内部网格定义中看到以下内容:

<Grid Height="{Binding ElementName=RootWindow, Path=ActualHeight}">

但是我无法做到这一点,因为我的RootWindow不在这个范围内,因为它位于不同的组件中(在XAML中)。但我了解到,我可以使用DependencyProperties对父Control的元素进行绑定,但是当我将Grid的Height设置为RootWindow的ActualHeight时,我甚至不知道它是否会起作用,其次我不知道如何实现这个DependencyProperty(我做了教程,但我无法使它工作(XAML无法识别我的依赖属性)。

问题:
有没有其他方法(没有依赖属性),以使DataGrid控制滚动? 如果不是:我如何实现这个依赖属性,以便它适用于我的情况?

0 个答案:

没有答案