嗨,有没有办法检查 Run 是否只是一个LineBreak
?
先解释一下
建议您在 a 中创建一些带有一些 LineBreaks 的文本,RichTextBox
现在让我们进一步建议,在您想将文本保存在数据库中之后,您可能会FlowDocument
像XML
我现在所做的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);
}
}
}