[英]WPF Richtextbox text selection
I have a WPF richtextbox having a Table as a Flowdocument. 我有一个WPF richtextbox,有一个表作为Flowdocument。 I need to copy content of selected table rows using C# on mouse right click. 我需要在鼠标右键单击时使用C#复制所选表行的内容。
The problem I m facing is being unable to get selected rows. 我面临的问题是无法获得选定的行。
Using following function but unable to get selected rows of Richtextbox. 使用以下功能但无法获取Richtextbox的选定行。
Any help would be appreciated. 任何帮助,将不胜感激。
Thanks in advance 提前致谢
public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection)
{
return document.Blocks.OfType<TableRow>().Where(w => selection.Contains(w.ContentStart)).ToList();
}
TableRow
instances are inside RowGroup
instances that are inside Table
instances. TableRow
实例都在里面RowGroup
实例是内Table
实例。 And tables are blocks. 表格是块。
You can try the code below: 您可以尝试以下代码:
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();
}
<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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.