簡體   English   中英

RichTextbox > 在插入符號 position 處插入 xaml

[英]RichTextbox > Insert xaml at caret position

我已經嘗試了幾天沒有運氣,我想要實現的是從字符串中添加流文檔 xaml 代碼。 字符串如:

string test = "<Paragraph><Run>Text</Run></Paragraph>";

但我不想在塊或文檔的末尾添加它,而是在當前插入符號 position 處添加。 我需要它能夠在我的 RichTextbox 中復制 UIElements。

感謝幫助 !

<Paragraph><Run>Text</Run></Paragraph>這樣的 XAML 文本不能直接插入到FlowDocument中。

它應該轉換為適當的Flow Related Classes

在您的情況下,創建Paragraph object 並將其插入當前插入符號 position:

MainWindows.xaml

<Window x:Class="WpfApp12.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"        
        mc:Ignorable="d"
        Topmost="True"
        Title="MainWindow" Height="350" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ToolBar Margin="0,0,0,0">
            <Menu VerticalAlignment="Center" Background="Transparent">                
                <MenuItem Header="Code Block" Click="InsertBlock_Click"/>                
            </Menu>
        </ToolBar>
        <Grid Grid.Row="1">
            <RichTextBox x:Name="rtb" Margin="5" IsDocumentEnabled="True">
                <FlowDocument>
                    <Paragraph Margin="0" Padding="0" FontSize="14" FontWeight="Bold">RichTextBox</Paragraph>                    
                    <Paragraph>A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content.</Paragraph>
                </FlowDocument>
            </RichTextBox>            
        </Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void InsertBlock_Click(object sender, RoutedEventArgs e)
    {
        // <Paragraph><Run>Text</Run></Paragraph>
        var paragraph = new Paragraph();
        paragraph.Inlines.Add(new Run("Text"));

        if (rtb.CaretPosition.IsEndOfBlock())
        {
            rtb.Document.Blocks.InsertAfter(rtb.CaretPosition.Paragraph, paragraph);
        }
        else
        {
            if (rtb.CaretPosition.IsAtLineStartPosition)
            {
                rtb.Document.Blocks.InsertBefore(rtb.CaretPosition.Paragraph, paragraph);
            }
            else
            {    
                rtb.Document.Blocks.InsertBefore(rtb.CaretPosition.InsertParagraphBreak().Paragraph, paragraph);
            }
        }
        rtb.Focus();
    }

    // Implementation of the `IsEndOfBlock()` extension method 
    public static class TextRangeExt
    {
        public static bool IsEndOfBlock(this TextPointer position)
        {
            for (; position != null; position = position.GetNextContextPosition(LogicalDirection.Forward))
            {
                switch (position.GetPointerContext(LogicalDirection.Forward))
                {
                    case TextPointerContext.ElementEnd:
                        if (position.GetAdjacentElement(LogicalDirection.Forward) is Paragraph) return true;
                        break;    
                    default:
                        return false;  
                }
            }
            return false;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM