如何获得带有文本环绕的文本块来填充视图框

Jon*_*ann 5 c# wpf viewbox word-wrap

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 pageheight = e.NewSize.Height;
    double pagewidth = e.NewSize.Width;
    double textHeight;
    double textWidth;

    textHeight = itemsControl.ActualHeight * scale.ScaleY;
    textWidth = itemsControl.ActualWidth * scale.ScaleX;

    if (textWidth == pagewidth)
    {
        itemsControl.Width = itemsControl.ActualWidth * .9;
    }
    else if (textHeight == pageheight)
    {
        itemsControl.Width = itemsControl.ActualWidth * 1.1;
    }
}
Run Code Online (Sandbox Code Playgroud)

McG*_*gle 3

我采纳了@user3284736的想法,将固定宽度应用于内部内容,并对其进行了扩展。

首先,向视图模型添加属性“ContainerWidth”,并通过处理SizeChanged视图事件来保持其更新:

public MyUserControl()
{
    SizeChanged += OnSizeChanged;
}

void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    ViewModel.ContainerWidth= e.NewSize.Width;
}
Run Code Online (Sandbox Code Playgroud)

现在将项目模板容器(示例中的“ParentGrid”)的宽度绑定到整个容器大小:

<Grid x:Name="ParentGrid" 
      Width="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.ContainerWidth}">
Run Code Online (Sandbox Code Playgroud)

设置固定大小会强制文本在 ViewBox 应用其缩放之前换行。

视框

编辑 这是我用来测试的 XAML:

<Grid x:Name="LayoutRoot" Background="Red">
    <Viewbox Stretch="Fill" StretchDirection="Both">
        <ItemsControl x:Name="itemsControl" ItemsSource="{Binding JournalEntries}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="ParentGrid" 
                          Width="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.ContainerWidth}">
                        <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 DetailedExplanation}" HorizontalAlignment="Left" TextWrapping="Wrap" FontSize="20" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Viewbox>
</Grid>
Run Code Online (Sandbox Code Playgroud)