Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将运行附加到FlowDocument中的现有段落?_C#_.net_Wpf - Fatal编程技术网

C# 如何将运行附加到FlowDocument中的现有段落?

C# 如何将运行附加到FlowDocument中的现有段落?,c#,.net,wpf,C#,.net,Wpf,我需要扩展现有的运行,以包括一些不同格式的新文本,而无需添加额外的段落。这可能吗 当我检查文档上第一个块的属性时,我没有看到任何属性允许我深入到段落中,以便向其添加运行 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Contro

我需要扩展现有的运行,以包括一些不同格式的新文本,而无需添加额外的段落。这可能吗

当我检查文档上第一个块的属性时,我没有看到任何属性允许我深入到段落中,以便向其添加运行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            this.flowdoc.Document = new FlowDocument();
            Run r = new Run("Hello ");
            r.Background = new SolidColorBrush(Colors.Yellow);
            r.FontSize = 14;

            Paragraph p = new Paragraph(r);
            flowdoc.Document.Blocks.Add(p);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Run r = new Run("World");
            r.Background = new SolidColorBrush(Colors.LightCyan);

            //Append run to existing run
            //
        }
    }
}

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <FlowDocumentReader Grid.Column="0" x:Name="flowdoc"></FlowDocumentReader>
        <Button Grid.Column="1" Content="append" Click="Button_Click"></Button>
    </Grid>
</Window>
要获取段落,可以迭代文档的Blocks属性。 然后,可以轻松地将新管路添加到块的Inlines集合中

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Run r = new Run("World");
        r.Background = new SolidColorBrush(Colors.LightCyan);

        //Append run to existing run
        var p = flowdoc.Document.Blocks.OfType<Paragraph>().First();
        p.Inlines.Add(r);
    }