【问题标题】:Grid doesn't fill available space when using DataTemplate使用 DataTemplate 时网格未填充可用空间
【发布时间】:2014-02-27 17:31:58
【问题描述】:

我有一个 ActiPro ThemedDataGrid(继承自 WPF DataGrid)。 我正在使用网格设置标题 DataTemplate,但它并没有占用所有可用空间。 这是我的数据模板

    <DataTemplate x:Key="MyKey" DataType="ViewModels:FieldVM">
        <Border BorderThickness="1" BorderBrush="Red">
            <Grid VerticalAlignment="Stretch" >
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Label}" DockPanel.Dock="Top" HorizontalAlignment="Center" Grid.Row="0"/>
                <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1,0,0,0" Background="{x:Null}" />
                <local:MyUserControl Name="units" VerticalAlignment="Bottom" DockPanel.Dock="Bottom" Visibility="Visible" Grid.Row="1"/>
        </Grid>
        </Border>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding AvailableUnits}" Value="{x:Null}" >
                <Setter Property="Visibility" Value="Collapsed" TargetName="units" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsUnitAware, RelativeSource={RelativeSource AncestorType={x:Type local:UnitConversionGrid}}}" Value="False" >
                <Setter Property="Visibility" Value="Collapsed" TargetName="units" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

我希望 Grid 使用所有可用空间,以便它们在 DataGrid 中的所有列中看起来均匀。

如何使 DataTemplate 中的 Grid 占用每列标题中的所有可用空间? 我希望行在所有列中使用相同的高度。 例如,如果第一列中行的高度是 30 和 70,那么我希望所有其他列具有相同的分布。

例子:

---------------------------------
| Row1 with  |Row1       | Row1  |
| More Text  |           |       |
|---------------------------------
| Row2 with  |Row2       |Row2   |
| More Text  |           |       |
----------------------------------

如何使列中的行占据较大行的高度?

谢谢,

另一个简化的例子:

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:controls="clr-namespace:ActiproSoftware.Windows.Controls.DataGrid;assembly=ActiproSoftware.DataGrid.Contrib.Wpf"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <controls:ThemedDataGrid x:Name="dataGrid" Grid.Row="0" Margin="0,0,10,0" VerticalAlignment="Top" RenderTransformOrigin="-10.556,-1.744" HorizontalAlignment="Right" Width="507" Height="310">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Width="50">
                    <DataGridCheckBoxColumn.Header>
                        <Grid VerticalAlignment="Stretch" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridColumnHeader}}, Path=Height}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock TextWrapping="Wrap" Width="40" DockPanel.Dock="Top">Value1 Test</TextBlock>
                            <ComboBox Grid.Row="1" Visibility="Collapsed"/>
                        </Grid>
                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridCheckBoxColumn>
                    <DataGridCheckBoxColumn.Header>
                        <Grid VerticalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock TextWrapping="Wrap" Width="50">Value2 Test With Four Lines</TextBlock>
                            <ComboBox Grid.Row="1"/>
                        </Grid>
                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridCheckBoxColumn>
                    <DataGridCheckBoxColumn.Header>
                        <Grid VerticalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="Value3" HorizontalAlignment="Center" DockPanel.Dock="Top" Grid.Row="0"/>
                            <ComboBox  VerticalAlignment="Bottom"  Grid.Row="1" Margin="0,1"/>
                        </Grid>
                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
            </DataGrid.Columns>
        </controls:ThemedDataGrid>
    </Grid>
</Window>

【问题讨论】:

  • 可能通过更改或删除共享大小组 A ,将其完全删除,它们将延伸到整个容器。
  • @eranotzap 我已经尝试不使用 SharedSizeGroup 属性但没有成功,Grid 每列的高度不同。
  • @Blam 第二行是 UserControl 不是标准的 WPF 组合框
  • 不知道我在哪里说组合框。你有列,行甚至没有排列。可以发张照片吗?
  • 您是否尝试将 Grid 替换为 DockPanel?

标签: c# wpf datagrid


【解决方案1】:

首先,检查以确保您的 DataGrid 与您想象的一样大(标题实际上可能填充正确)。

接下来,尝试将 Grid 的 width 属性绑定到 DataGrid 的宽度,如下所示:

   Width="{Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" Path="ActualWidth"}"

您可能需要对保持边框元素执行相同的操作。

您的 XAML 还包含一些 DockPanel 属性,如果没有停靠面板容器,这些属性似乎不合适。这不应该破坏任何东西,但你可能还是想删除它。

【讨论】:

  • 我认为您的方向是正确的,但是这会使标题采用整个 DataGrid 的高度(我从您的回复中替换了宽度),所以我的 DataGrid 只显示标题。我尝试在 AncestorType 中使用 DataGridColumnHeader 但得到了相同的行为。应该是哪个 AncestorType?
  • 我添加了一个简化示例以更好地查看问题。如果您没有 ActiPro,您可以轻松地将 ThemedDataGrid 替换为 DataGrid
  • 我很清楚,在您的新示例中,问题是“较短”标题上的文本不在顶部?或者它不跨越网格空间?其他一切看起来都像你的照片。
  • 问题是第一行中的文本在所有列之间不对齐,并且对于第二行(组合框)的内容相同。我想要这样做的方法是将第一行中的文本对齐到标题的顶部,将第二行(组合框)对齐到标题的底部,以确保所有文本和组合框在列之间处于同一级别。
  • 在对此进行调试时,我浏览了 VisualTree,并注意到我的标题内的网格(在我的 xaml 中)是在将 VerticalAlignment 设置为“Center”的 ContenPresenter 内创建的。有没有办法通过 XAML 改变它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多