TextBlock由换行引起的内联行为问题

本文关键字:问题 换行 TextBlock | 更新日期: 2023-09-27 18:03:25

有没有办法检查一个Run是否只是一个LineBreak ?

先解释一下

建议您在RichTextBox中创建一些具有断行符的文本,现在让我们进一步建议,在您想将文本保存在数据库中之后,您可能会将FlowDocument转换为XML我现在如何在TextBlock中显示此"xml字符串",因此我将其转换为FlowDocument编写GetAllLine扩展并使用附加行为绑定到TextBlock.Inlines结束这里我的断行问题发生<Run xml:lang='de-de' xml:space='preserve' />不会导致LineBreak

现在我得到的是

XAML

<Window x:Class="TextBlockAttachedIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TextBlockAttachedIssue"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock local:Bindable.Inlines="{Binding myInlines}" TextWrapping="WrapWithOverflow"/>
    </Grid>
</Window>
后台代码
namespace TextBlockAttachedIssue
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new VM();
        }
    }
    public class VM
    {
        private IEnumerable<Inline> _myInlines;
        public VM()
        {
            var myParagraph =
                "<Paragraph xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> " +
                "<Run xml:lang='de-de' xml:space='preserve' />" +
                "<Run xml:lang='de-de' xml:space='preserve' />" +
                    " das ist text davor" +
                    "<Run FontFamily='Palatino Linotype'> " +
                        "line2  dsf adsgf sd fds gs fd gsfd g sdfg df h g hdgf h fg hhgfdh gfh " +
                    "</Run> " +
                "<Run xml:lang='de-de' xml:space='preserve' />" +
                    "und das ist text danach" +
                "</Paragraph> ";
            var para = XamlReader.Load(XmlReader.Create(new StringReader(myParagraph))) as Paragraph;
            myInlines = para.Inlines.ToList();
        }
        public IEnumerable<Inline> myInlines
        {
            get { return _myInlines; }
            private set { _myInlines = value; }
        }
    }
}

附加行为
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TextBlockAttachedIssue
{
    public static class Bindable
    {
        public static readonly DependencyProperty InlinesProperty = DependencyProperty.RegisterAttached("Inlines", typeof(IEnumerable<Inline>), typeof(Bindable), new PropertyMetadata(OnInlinesChanged));
        private static void OnInlinesChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var textBlock = source as TextBlock;
            if (textBlock != null)
            {
                textBlock.Inlines.Clear();
                var inlines = e.NewValue as IEnumerable<Inline>;
                if (inlines != null)
                    textBlock.Inlines.AddRange(inlines);
            }
        }
        [AttachedPropertyBrowsableForType(typeof(TextBlock))]
        public static IEnumerable<Inline> GetInlines(this TextBlock textBlock)
        {
            return (IEnumerable<Inline>)textBlock.GetValue(InlinesProperty);
        }
        public static void SetInlines(this TextBlock textBlock, IEnumerable<Inline> inlines)
        {
            textBlock.SetValue(InlinesProperty, inlines);
        }
    }
}

TextBlock由换行引起的内联行为问题

我不认为Run代表换行。为了表示换行,使用了不同类型的Inline - LineBreak。

你的文本没有任何换行时,在FlowDocumentReader查看:

<FlowDocumentReader>
    <FlowDocument>
        <Paragraph xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
            <Run xml:lang='de-de' xml:space='preserve' />
            <Run xml:lang='de-de' xml:space='preserve' /> das ist text davor
            <Run FontFamily='Palatino Linotype'> 
                line2  dsf adsgf sd fds gs fd gsfd g sdfg df h g hdgf h fg hhgfdh gfh 
            </Run> 
            <Run xml:lang='de-de' xml:space='preserve' />
            und das ist text danach
        </Paragraph>
    </FlowDocument>
</FlowDocumentReader>
另一方面,

Paragraph是一个Block元素,它会跳转到新行。也许是段落导致了你的问题。

为什么不使用FlowDocumentReader显示FlowDocument文本,而不是TextBlock?使用FlowDocumentReader,您将完全支持FlowDocument,包括段落和所有其他textelement派生的项目。