由 LineBreak 引起的 TextBlock 内联附加行为问题 [英] TextBlock Inlines Attached Behavior Issue by LineBreak
问题描述
有没有办法检查 Run 是否只是 LineBreak
?
Hi is there a way to check if a Run is just a LineBreak
?
建议您在 RichTextBox
中创建一些带有一些 LineBreaks 的文本,现在让我们进一步建议您想将文本保存在数据库中后,您可能会将 FlowDocument
转换为 XML
我现在是怎么做的,我想在 TextBlock
中显示这个XML 字符串",所以我将它转换回 FlowDocument 编写 GetAllLine 扩展并使用附加行为绑定到TextBlock.Inlines
在这里结束我的 LineBreak 问题发生 <Run xml:lang='de-de' xml:space='preserve'/>
不会导致一个 LineBreak
.
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
.
<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);
}
}
}
推荐答案
我不相信 Run 曾经代表换行.为了表示换行符,不同种类的内联 使用 - LineBreak.
I do not believe that Run ever represents a line break. To represent a line break, different kind of Inline is used - LineBreak.
<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>
段落,另一方面是一个 Block 元素,它打破了一个新的线.也许段落导致了您的问题.
Paragraph, on the other hand is a Block element which breaks to a new line. Perhaps Paragraph is causing your issue.
为什么不使用 FlowDocumentReader 显示 FlowDocument 文本,而不是 TextBlock?使用 FlowDocumentReader,您将完全支持 FlowDocument,包括段落和所有其他 TextElement-派生的项.
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.
这篇关于由 LineBreak 引起的 TextBlock 内联附加行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!