使用DataTemplate时,网格不会填充可用空间

时间:2014-02-27 17:31:58

标签: c# wpf datagrid

我有一个ActiPro ThemedDataGrid(继承自WPF DataGrid)。 我正在使用Grid设置标头DataTemplate,但它没有占用所有可用空间。 这是我的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>

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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