如何根据另一个DataGrid的高度扩展DataGrid的高度?

发布于 2024-10-15 19:19:56 字数 3385 浏览 2 评论 0原文

我希望能够基于具有更多数据并因此变得更高的 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 DataGrids based on the DataGrid that have more data and become higher because of it. In other words, I’d like to have two DataGrids 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-10-22 19:19:56

您可以将两个 DataGrid 放入一个 Grid 中,每行一行,使用 SharedSizeGroup 让它们同步。设置 Grid.IsSharedSizeScope 为两个网格的父网格。

例如这样的东西

<Grid x:Name="LayoutRoot" Margin="10" Grid.IsSharedSizeScope="True">
    ...
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="A"/>
        </Grid.RowDefinitions>
        ...
        <DataGrid .../> <!-- DataGrid 1 -->
    </Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="A"/>
        </Grid.RowDefinitions>
        ...
        <DataGrid .../> <!-- DataGrid 2 -->
    </Grid>
    ...
</Grid>

You can put both DataGrids into a Grid with one row each, use SharedSizeGroup to have them sync. Set Grid.IsSharedSizeScope for a parent of both grids.

e.g. something like this

<Grid x:Name="LayoutRoot" Margin="10" Grid.IsSharedSizeScope="True">
    ...
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="A"/>
        </Grid.RowDefinitions>
        ...
        <DataGrid .../> <!-- DataGrid 1 -->
    </Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="A"/>
        </Grid.RowDefinitions>
        ...
        <DataGrid .../> <!-- DataGrid 2 -->
    </Grid>
    ...
</Grid>
明月松间行 2024-10-22 19:19:56

如果我理解正确的话,您需要让两个 DataGrid 具有完全相同的高度吗?尝试将一个 DataGridMinimumHeight 设置为另一个 DataGrid 的 ActualHeight,反之亦然。这可能会起作用:

<DataGrid x:Name="Main" 
    MinHeight="{Binding ElementName=Main1,Path=ActualHeight}"
    ...

<DataGrid x:Name="Main1" GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
    MinHeight="{Binding ElementName=Main,Path=ActualHeight}"
    ...

If I understand you right, you need to have both DataGrids to have the exactly same height? Try setting the MinimumHeight of one DataGrid to the ActualHeight of the other and vice versa. This may do the trick:

<DataGrid x:Name="Main" 
    MinHeight="{Binding ElementName=Main1,Path=ActualHeight}"
    ...

<DataGrid x:Name="Main1" GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
    MinHeight="{Binding ElementName=Main,Path=ActualHeight}"
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文