XAML 是一个页面,其中包含 Viewbox 内的项目列表。目标是有一个部门名称,并在其下面有一段注释。我将 itemscontrol 的背景设置为红色,以便轻松查看它如何填充页面。
<Grid >
<Viewbox x:Name="slideViewBox">
<ItemsControl x:Name="itemsControl" Background="Red" ItemsSource="{Binding JournalEntries}" Grid.IsSharedSizeScope="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="ParentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" HorizontalAlignment="Left" Foreground="White" Text="{Binding Department}" FontSize="32"/>
<TextBlock x:Name="detailsTextBlock" Grid.Row="1" Foreground="White" Text="{Binding DetailedExplaination}" HorizontalAlignment="Left" TextWrapping="Wrap" FontSize="20" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Viewbox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
它生成什么:
我理想中想要的:

请注意,在第一张图片中,详细信息 TextBlock 根本没有自动换行,并且浪费了页面上的大量可用空间。
我确实在 Size_Changed 事件处理程序中编写了一些代码,这让我更接近我的目标,但并非完全实现。我手动调整 itemscontrol 宽度以强制文本换行。我只需要让它自动执行此操作,以便在页面加载时看起来像第二张图片。
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
var child = VisualTreeHelper.GetChild(slideViewBox, 0) as ContainerVisual;
var scale = child.Transform as ScaleTransform;
double …Run Code Online (Sandbox Code Playgroud)