Scenario:
我有一个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>
Problem:
我希望DataGrid控制滚动,但因为在contentcontrol周围有一个scrollviewer,DataGrid周围的组件认为,它们具有无限高度,所以外部ScrollViewer控制滚动 .
Possible solution:
Here我看了:
您需要在内部ScrollViewer上设置一个高度,否则它将根据内容的大小拉伸所需的数量 .
在代码示例中,我无法在内部滚动条中看到任何内容:
<ScrollViewer Grid.Row =“1”>
<DataGrid /> </的ScrollViewer>
但我能够在内部网格定义中看到以下内容:
<Grid Height =“{Binding ElementName = RootWindow,Path = ActualHeight}”>
但我不能这样做,因为我的RootWindow不在这个范围内,因为它位于不同的组件中(在XAML中) . 但我了解到,我可以使用DependencyProperties对父Control的元素进行绑定,但是当我将Grid的Height设置为RootWindow的ActualHeight时,我甚至不知道它是否会起作用,其次我不知道如何实现这个DependencyProperty(我做了教程,但我无法使它工作(XAML无法识别我的依赖属性) .
Question:
有没有其他方法(没有依赖属性),以使DataGrid控制滚动?如果没有:我如何实现这个依赖属性,以便它适用于我的情况?