【问题标题】:WPF Richtextbox text selectionWPF Richtextbox 文本选择
【发布时间】:2014-11-02 22:11:19
【问题描述】:

我有一个 WPF 富文本框,其中有一个表作为流文档。我需要在鼠标右键单击时使用 C# 复制选定表格行的内容。

我面临的问题是无法获取选定的行。

使用以下函数但无法获取 Richtextbox 的选定行。

任何帮助将不胜感激。

提前致谢

public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection)
    {
        return document.Blocks.OfType<TableRow>().Where(w => selection.Contains(w.ContentStart)).ToList();
    }

【问题讨论】:

    标签: c# wpf richtextbox textselection


    【解决方案1】:

    TableRow 实例位于 RowGroup 实例中,而 Table 实例位于 Table 实例中。表格就是块。

    你可以试试下面的代码:

    public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection)
    {
        return document.Blocks
            .OfType<Table>()
            .SelectMany(x => x.RowGroups)
            .SelectMany(x => x.Rows)
            .Where(w => selection.Contains(w.ContentStart))
            .ToList();
    }
    

    更新:完整示例代码

    Xaml

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Blend.MainWindow">
        <StackPanel>
            <RichTextBox Name="RichTextBox" SelectionChanged="OnSelectionChanged">
                <FlowDocument>
                    <Table CellSpacing="0">
                        <Table.Columns>
                            <TableColumn Width="50" />
                            <TableColumn Width="50" />
                        </Table.Columns>
                        <TableRowGroup>
                            <TableRow>
                                <TableCell BorderThickness="1" BorderBrush="Black">
                                    <Paragraph>
                                        <Run>1</Run>
                                    </Paragraph>
                                </TableCell>
                                <TableCell BorderThickness="1" BorderBrush="Black">
                                    <Paragraph>
                                        <Run>2</Run>
                                    </Paragraph>
                                </TableCell>
                            </TableRow>
                            <TableRow>
                                <TableCell BorderThickness="1" BorderBrush="Black">
                                    <Paragraph>
                                        <Run>3</Run>
                                    </Paragraph>
                                </TableCell>
                                <TableCell BorderThickness="1" BorderBrush="Black">
                                    <Paragraph>
                                        <Run>4</Run>
                                    </Paragraph>
                                </TableCell>
                            </TableRow>
                        </TableRowGroup>
                    </Table>
                    <Paragraph />
                </FlowDocument>
            </RichTextBox>
            <TextBlock><Run>Rows selected: </Run><Run Name="TableRowCount" /></TextBlock>
            <TextBlock><Run>Selection Text: </Run><Run Name="SelectionText" /></TextBlock>
        </StackPanel>
    </Window>
    

    代码隐藏

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Documents;
    
    namespace Blend
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void OnSelectionChanged(object sender, RoutedEventArgs e)
            {
                TableRowCount.Text = GetSelectedParagraphs(RichTextBox.Document, RichTextBox.Selection).Count.ToString();
                SelectionText.Text = RichTextBox.Selection.Text;
            }
    
            public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection)
            {
                return document.Blocks
                    .OfType<Table>()
                    .SelectMany(x => x.RowGroups)
                    .SelectMany(x => x.Rows)
                    .Where(w => selection.Contains(w.ContentStart))
                    .ToList();
            }
        }
    }
    

    【讨论】:

    • 感谢 filhit,但另一个问题是我无法获得丰富的文本框选择。 m 使用 TextSelection txtSel = rtxtBox.Selection;但是将 txtSel.Text 设置为空。如何获取richtextbox表的选定行???
    • @AshishBathla 我添加了完整的示例。这对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2015-11-12
    • 1970-01-01
    相关资源
    最近更新 更多