TextBlock Inlines Attached Behavior Issue by LineBreak

大城市里の小女人 提交于 2019-12-12 03:44:35

问题


Hi is there a way to check if a Run is just a 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.

now here is what i got so far

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

回答1:


I do not believe that Run ever represents a line break. To represent a line break, different kind of Inline is used - LineBreak.

Your text doesn't have any line breaks when viewed in 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. Perhaps Paragraph is causing your issue.

Why don't you use FlowDocumentReader to show FlowDocument text, instead of TextBlock? With FlowDocumentReader you would have full support for FlowDocument, including Paragraphs and all other TextElement-derived items.



来源:https://stackoverflow.com/questions/15362815/textblock-inlines-attached-behavior-issue-by-linebreak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!