我最近一直在試圖讓WPF(C/4.0)DataGrid中的文本包裝工作,並且無論我實現哪種解決方案(都在包裝中使用某種形式的TextBlock)網格的自動高度,並導致網格底部出現過多的空白(爲了可見性而設置爲黃色)。WPF DataGrid AutoSize問題
我的代碼:(註釋代碼是替代解決方案文本換行,但仍導致過度的空間)
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" AutoGenerateColumns="False" ColumnWidth="*" Name="dgFamilyHistories" IsReadOnly="True"
HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=Patient.FamilyHistories}" RowDetailsVisibilityMode="Visible"
GridLinesVisibility="All">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="Auto"/>
</Style>
<!--<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="border">
<ContentControl Content="{TemplateBinding Content}">
<ContentControl.ContentTemplate>
<DataTemplate>
<DockPanel>
<TextBlock TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis"
Width="Auto" Height="Auto" Text="{Binding Text}"/>
</DockPanel>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
</DataGrid.Resources>
<DataGrid.Background>
<SolidColorBrush Color="Yellow" />
</DataGrid.Background>
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding DateEntered, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/>
<!--<DataGridTextColumn Header="Relation" Binding="{Binding Relation}"/>-->
<DataGridTemplateColumn Header="Relation">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Relation}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Header="Illness" Binding="{Binding Illness}"/>-->
<DataGridTemplateColumn Header="Illness">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Illness}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Header="Health" Binding="{Binding Health}"/>-->
<DataGridTemplateColumn Header="Health">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Health}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Birth Date" Binding="{Binding DateOfBirth, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/>
<DataGridTextColumn Header="Death Date" Binding="{Binding DateOfDeath, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/>
<!--<DataGridTextColumn Header="Death Cause" Binding="{Binding CauseOfDeath}"/>-->
<DataGridTemplateColumn Header="Death Cause">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=CauseOfDeath}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="50"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Label Name="lblDetails" Content="{Binding Path=Comments}" ContentStringFormat="{}Comments: {0}" Margin="15,0,0,0"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Comments, Converter={Converters:IsNullStringConverter}}" Value="True">
<Setter TargetName="lblDetails" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
<DockPanel Grid.Row="1" Background="Blue">
</DockPanel>
</Grid>
此方法適用於每一個的DataGridColumn,但汽車大小在使用流程文檔打印時仍然會感到困惑。如果流程文檔觸發調整其高度顯示正確,但所有列均已壓平。 – 2010-11-07 20:58:48
看到你批准了一段時間後被拒絕,所以我覺得有些東西沒有打球。所以列最終寬度最小或效果如何? – 2010-11-07 21:04:46
非常多,basiclly我正在把網格放在一個流動文檔中,然後我打印它。本質上發生的是,只要流文檔超過一頁,其上的第一個數據網就會混亂。 (整個網格是正確的寬度,標題也是如此,但腳趾細胞全部壓扁到左側,我嘗試併發佈一個屏幕截圖 – 2010-11-07 21:18:34