简体   繁体   English

TextBlock内联LineBreak附加行为问题

[英]TextBlock Inlines Attached Behavior Issue by LineBreak

Hi is there a way to check if a Run is just a LineBreak ? 嗨,有没有办法检查Run是否只是LineBreak

Some explenation first 首先有些夸张

suggesting you create some Text with some LineBreaks in a RichTextBox now let's further suggest after you want to save your text in a DataBase you will probably convert the FlowDocument as XML how i did now i want to show this "XML-string" in a TextBlock so i convert it back as FlowDocument write an GetAllLine extension and used a Attached Behavior to bind to TextBlock.Inlines end here my LineBreak Issue occurs <Run xml:lang='de-de' xml:space='preserve' /> doesn't result in a LineBreak . 建议您在RichTextBox创建带有一些LineBreaks的文本,现在让我们进一步建议,在将文本保存到数据库中之后,您可能将FlowDocument转换为XML方式现在我想在TextBlock显示此“ XML字符串”所以我将其转换为FlowDocument编写的GetAllLine扩展名并使用“附加行为”绑定到TextBlock.Inlines在这里结束我的LineBreak问题<Run xml:lang='de-de' xml:space='preserve' />不会“导致LineBreak

now here is what i got so far 现在这是我到目前为止所得到的

XAML 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>

CodeBehind 代码背后

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; }
        }
    }
}

Attached Behavior 依附行为

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);
        }
    }
}

I do not believe that Run ever represents a line break. 我认为Run永远不会代表换行符。 To represent a line break, different kind of Inline is used - LineBreak . 为了表示换行符,使用了另一种Inline - LineBreak

Your text doesn't have any line breaks when viewed in FlowDocumentReader : 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 , on the other hand is a Block element which breaks to a new line. 另一方面, 段落是一个Block元素,它会换行。 Perhaps Paragraph is causing your issue. 也许是段落导致了您的问题。

Why don't you use FlowDocumentReader to show FlowDocument text, instead of TextBlock? 为什么不使用FlowDocumentReader而不是TextBlock显示FlowDocument文本? With FlowDocumentReader you would have full support for FlowDocument, including Paragraphs and all other TextElement -derived items. 使用FlowDocumentReader,您将完全支持FlowDocument,包括段落和所有其他TextElement派生的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM