0

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

先解释一下

建议您在 a 中创建一些带有一些 LineBreaks 的文本,RichTextBox现在让我们进一步建议,在您想将文本保存在数据库中之后,您可能会FlowDocumentXML我现在所做的TextBlock那样转换回到 FlowDocument 编写 GetAllLine 扩展并使用附加行为绑定到TextBlock.Inlines此处结束我的 LineBreak 问题发生<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);
        }
    }
}
4

1 回答 1

1

我不相信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元素。也许 Paragraph 引起了您的问题。

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

于 2013-03-12T15:50:00.337 回答