xaml - 在水平堆栈面板或包装面板中很好地包装文本?

标签 xaml windows-phone-8 stackpanel word-wrap wrappanel

我看过其他一些关于此的帖子,但没有看到任何可以解决我的问题的帖子。基本上,我需要将一些文本(粗体和非粗体)很好地包裹在 StackpanelWrappanel 中。这是一个视觉效果:

enter image description here

我需要您看到的两个 TextBlocks 的组合。我有第一个粗体 TextBlock,其中包含“Title:”,然后在同一行上我需要下一个 TextBlock,它可能会换行,也可能不会换行。如果确实换行,我需要顶行保持在顶行然后换行,就好像它们都是一个 TextBlock 一样。这是我需要的自制油漆视觉效果:

enter image description here

这是我到目前为止的代码:

<toolkit:WrapPanel Orientation="Horizontal">
   <TextBlock Text="Title: " FontWeight="Bold"/>
   <TextBlock TextWrapping="Wrap" Text="More text goes here " />
</toolkit:WrapPanel>

<toolkit:WrapPanel Orientation="Horizontal">
   <TextBlock Text="Title: " FontWeight="Bold"/>
   <TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</toolkit:WrapPanel>

现在,我反对在one TextBlock 中完成此操作(如果可能)。我知道使用 TextBlock.Inlines 我可以添加一个 Run 粗体文本,然后添加另一个常规文本。问题是 Inlines cannot be bound使用 MVVM(我没有在本次演示中使用,但将在最终代码中使用)。

因此无论解决方案是什么,我都需要能够从代码隐藏中设置 TextBlock 的值,以便我知道可以使用 MVVM 完成。

有谁知道实现这个的方法吗?

最佳答案

使用 RichTextBox 控件对每种字体样式进行不同的运行。

<RichTextBox>
    <Paragraph>
        <Run FontWeight="Bold">Title:</Run>
        <Run>More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :(</Run>
    </Paragraph>
</RichTextBox>

您可以将 Run 元素的 Text 属性绑定(bind)到您的数据上下文,例如:

    <RichTextBox>
        <Paragraph>
            <Run FontWeight="Bold"
                 Text="{Binding Header}"></Run>
            <Run Text="{Binding Text}"></Run>
        </Paragraph>
    </RichTextBox>

关于xaml - 在水平堆栈面板或包装面板中很好地包装文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567153/

相关文章:

c++ - 请解释为什么文件路径的开头有一个S?

c# - WPF 的 MVVM 模式 - 不使用任何工具包的二维图

windows-phone-8 - Windows Phone 8 磁贴不显示正确的图像

c# - 使用数据库中的复选框填充 StackPanel

wpf - 如何使另一个 StackPanel 的 StackPanel 宽度?

mvvm - 使用 MVVM 以编程方式使用用户控件的可观察集合填充堆栈面板

c# - 如何在 WPF XAML 中使用行索引作为 DataGridCell 样式的多重触发器中的条件?

c# - MVVM 绑定(bind)到数组索引

c# - IEnumerator 的随机顺序

c# - 使用 javascript (JQuery 请求) 将通知推送到 wp8