C# 文本块内联线按换行符附加的行为问题
嗨,有没有办法检查一次跑步是否只是一次换行 先解释一下 建议您在C# 文本块内联线按换行符附加的行为问题,c#,wpf,xaml,mvvm,attachedbehaviors,C#,Wpf,Xaml,Mvvm,Attachedbehaviors,嗨,有没有办法检查一次跑步是否只是一次换行 先解释一下 建议您在RichTextBox中创建一些带有换行符的文本,现在让我们进一步建议,在您希望将文本保存到数据库中后,您可能会将FlowDocument转换为XML我现在想如何显示此“XML字符串”在TextBlock中,我将其转换回FlowDocument,编写一个GetAllLine扩展,并使用附加的行为绑定到TextBlock。内联线在这里结束,我的换行符问题发生不会导致换行符 现在我得到的是 XAML 代码隐藏 命名空间TextBlo
RichTextBox
中创建一些带有换行符的文本,现在让我们进一步建议,在您希望将文本保存到数据库中后,您可能会将FlowDocument
转换为XML
我现在想如何显示此“XML字符串”在TextBlock
中,我将其转换回FlowDocument,编写一个GetAllLine扩展,并使用附加的行为绑定到TextBlock。内联线
在这里结束,我的换行符问题发生
不会导致换行符
现在我得到的是
XAML
代码隐藏
命名空间TextBlockAttachedIssue
{
///
///Interaktionslogik für MainWindow.xaml
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=newvm();
}
}
公共类虚拟机
{
私有IEnumerable_myInlines;
公共虚拟机()
{
var My段落=
" " +
"" +
"" +
“das ist text davor”+
" " +
“线路2 dsf adsgf sd fds gs fd gsfd g sdfg df h g hdgf h fg hhgfdh gfh”+
" " +
"" +
“und das ist text danach”+
" ";
var para=XamlReader.Load(XmlReader.Create(newstringreader(my段落)))作为段落;
myInlines=para.Inlines.ToList();
}
公共可数myinline
{
获取{return\u myInlines;}
私有集{u myInlines=value;}
}
}
}
依附行为
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
命名空间TextBlockAttachedIssue
{
公共静态类可绑定
{
public static readonly dependencProperty InlinesProperty=dependencProperty.RegisterAttached(“Inlines”、typeof(IEnumerable)、typeof(Bindable)、new PropertyMetadata(OnInlinesChanged));
InLinesChanged上的私有静态无效(DependencyObject源,DependencyPropertyChangedEventArgs e)
{
var textBlock=源作为textBlock;
if(textBlock!=null)
{
textBlock.Inlines.Clear();
var inlines=e.NewValue作为IEnumerable;
如果(内联线!=null)
textBlock.Inlines.AddRange(Inlines);
}
}
[AttachedPropertyBrowsableForType(typeof(TextBlock))]
公共静态IEnumerable GetInlines(此TextBlock TextBlock)
{
return(IEnumerable)textBlock.GetValue(InlinesProperty);
}
公共静态void SetInlines(此TextBlock TextBlock,IEnumerable inlines)
{
设置值(InlinesProperty,inlines);
}
}
}
我不认为这代表断线。为了表示换行符,使用了不同类型的-
在以下位置查看文本时,文本没有任何换行符:
das ist text davor
线路2 dsf adsgf sd fds gs fd gsfd g sdfg df h g hdgf h fg HHGFD GFDH gfh
这是一本书
,另一方面,是一个元素,它断开到新行。也许这段话引起了你的问题
为什么不使用显示FlowDocument文本而不是TextBlock?
有了它,您将完全支持FlowDocument,包括段落和所有其他派生项。因为我不知道有
FlowDocumentReader
存在,我将查看它,但我确信我得到了我需要的答案:o)。但是,如果我的字符串在FlowDocumentReader
中确实不包含任何LineBreak
,为什么XamlWriter.Save()
会将我的RichTextBox
中的分线符转换为
最终使用FlowDocumentScrollViewer
<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);
}
}
}
<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>