我有一个 ActiPro ThemedDataGrid(继承自 WPF DataGrid)。 我正在使用网格设置 header 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>
我希望网格使用所有可用空间,以便它们在 DataGrid 中的所有列中看起来均匀。
如何使 DataTemplate 中的网格占用每列标题中的所有可用空间? 我希望行在所有列中使用相同的高度。 例如,如果第一列的行高分别为 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>
最佳答案
首先,检查以确保您的 DataGrid 与您认为的一样大(标题可能实际上已正确填充)。
接下来,尝试将 Grid 的 width 属性绑定(bind)到 DataGrid 的宽度,如下所示:
Width="{Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" Path="ActualWidth"}"
您可能需要对固定边框元素执行相同的操作。
您的 XAML 还包含一些 DockPanel 属性,如果没有停靠面板容器,这些属性似乎不合适。那不应该破坏任何东西,但是您可能还是想将其删除。
关于c# - 使用 DataTemplate 时网格不会填充可用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22075966/