如何根据另一个DataGrid的高度扩展DataGrid的高度?
我希望能够基于具有更多数据并因此变得更高的 DataGrid
使两个 DataGrid
具有相同的高度。换句话说,我希望两个 DataGrid 的高度相同,无论数据输入如何。我有一行和几列,其中一个 DataGrid
占用一列,另一个 DataGrid
占用另一列。万一,DataGrid
之一获得更多输入,然后它会延伸得更高。我希望另一个 DataGrid
具有相同的高度,下面有空白区域。我想知道如何才能让它发挥作用。任何 DataGrid
都应根据较高的 DataGrid
的高度自动扩展。任何想法都受到高度赞赏!
下面是示例 XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="DataGridExpand.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<XmlDataProvider x:Key="Info" XPath="Info/Values">
<x:XData>
<Info xmlns="">
<Values Name="Value1" />
<Values Name="Value2" />
<Values Name="Value3" />
<Values Name="Value4" />
<Values Name="Value5" />
<Values Name="Value6" />
<Values Name="Value7" />
<Values Name="Value8" />
<Values Name="Value9" />
</Info>
</x:XData>
</XmlDataProvider>
<XmlDataProvider x:Key="Info2" XPath="Info2/Values2">
<x:XData>
<Info2 xmlns="">
<Values2 Name="Value1" />
<Values2 Name="Value2" />
<Values2 Name="Value3" />
<Values2 Name="Value4" />
<Values2 Name="Value5" />
</Info2>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid x:Name="LayoutRoot" Margin="10" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="49*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="49*" />
</Grid.ColumnDefinitions>
<Border Padding="10" BorderBrush="Black" Background="#FFD2D2D2">
<DataGrid x:Name="Main" GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
ItemsSource="{Binding XPath=/Info2/Values2}"
DataContext="{Binding Source={StaticResource Info2}}" Margin="10">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" Width="160" />
</DataGrid.Columns>
</DataGrid>
</Border>
<GridSplitter x:Name="GridSplitter" ResizeBehavior="PreviousAndNext" HorizontalAlignment="Stretch" Grid.Column="1" />
<Border Grid.Column="2" Padding="10" BorderBrush="Black" Background="#FFD2D2D2">
<DataGrid x:Name="Main1" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" Margin="10"
ItemsSource="{Binding XPath=/Info/Values}"
DataContext="{Binding Source={StaticResource Info}}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" Width="160" />
</DataGrid.Columns>
</DataGrid>
</Border>
</Grid>
I’d like to be able have equal heights of two DataGrid
s based on the DataGrid
that have more data and become higher because of it. In other words, I’d like to have two DataGrid
s be the same in heights regardless data input. I have one row and few columns where one DataGrid
takes one column and another DataGrid
takes another column. In case, one of DataGrid
gets more input then it extends taller. I want another DataGrid
to be the same tall with empty space below. I am wondering how I can make it work. Any DataGrid
should expand automatically based on the height of taller DataGrid
. Any ideas are highly appreciated!
Below is sample XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="DataGridExpand.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<XmlDataProvider x:Key="Info" XPath="Info/Values">
<x:XData>
<Info xmlns="">
<Values Name="Value1" />
<Values Name="Value2" />
<Values Name="Value3" />
<Values Name="Value4" />
<Values Name="Value5" />
<Values Name="Value6" />
<Values Name="Value7" />
<Values Name="Value8" />
<Values Name="Value9" />
</Info>
</x:XData>
</XmlDataProvider>
<XmlDataProvider x:Key="Info2" XPath="Info2/Values2">
<x:XData>
<Info2 xmlns="">
<Values2 Name="Value1" />
<Values2 Name="Value2" />
<Values2 Name="Value3" />
<Values2 Name="Value4" />
<Values2 Name="Value5" />
</Info2>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid x:Name="LayoutRoot" Margin="10" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="49*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="49*" />
</Grid.ColumnDefinitions>
<Border Padding="10" BorderBrush="Black" Background="#FFD2D2D2">
<DataGrid x:Name="Main" GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
ItemsSource="{Binding XPath=/Info2/Values2}"
DataContext="{Binding Source={StaticResource Info2}}" Margin="10">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" Width="160" />
</DataGrid.Columns>
</DataGrid>
</Border>
<GridSplitter x:Name="GridSplitter" ResizeBehavior="PreviousAndNext" HorizontalAlignment="Stretch" Grid.Column="1" />
<Border Grid.Column="2" Padding="10" BorderBrush="Black" Background="#FFD2D2D2">
<DataGrid x:Name="Main1" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" Margin="10"
ItemsSource="{Binding XPath=/Info/Values}"
DataContext="{Binding Source={StaticResource Info}}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" Width="160" />
</DataGrid.Columns>
</DataGrid>
</Border>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将两个
DataGrid
放入一个Grid
中,每行一行,使用SharedSizeGroup
让它们同步。设置 Grid.IsSharedSizeScope 为两个网格的父网格。例如这样的东西
You can put both
DataGrids
into aGrid
with one row each, useSharedSizeGroup
to have them sync. SetGrid.IsSharedSizeScope
for a parent of both grids.e.g. something like this
如果我理解正确的话,您需要让两个 DataGrid 具有完全相同的高度吗?尝试将一个
DataGrid
的MinimumHeight
设置为另一个 DataGrid 的ActualHeight
,反之亦然。这可能会起作用:If I understand you right, you need to have both
DataGrid
s to have the exactly same height? Try setting theMinimumHeight
of oneDataGrid
to theActualHeight
of the other and vice versa. This may do the trick: