C# WPF Richtextbox文本选择
我有一个WPF richtextbox,其中有一个表作为流程文档。我需要在鼠标右键单击时使用C#复制选定表行的内容 我面临的问题是无法获取所选行 正在使用以下函数,但无法获取Richtextbox的选定行 任何帮助都将不胜感激 提前谢谢C# WPF Richtextbox文本选择,c#,wpf,richtextbox,textselection,C#,Wpf,Richtextbox,Textselection,我有一个WPF richtextbox,其中有一个表作为流程文档。我需要在鼠标右键单击时使用C#复制选定表行的内容 我面临的问题是无法获取所选行 正在使用以下函数,但无法获取Richtextbox的选定行 任何帮助都将不胜感激 提前谢谢 public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection) { return docum
public static List<TableRow> GetSelectedParagraphs(FlowDocument document, TextSelection selection)
{
return document.Blocks.OfType<TableRow>().Where(w => selection.Contains(w.ContentStart)).ToList();
}
公共静态列表GetSelected段落(流程文档、文本选择)
{
return document.Blocks.OfType().Where(w=>selection.Contains(w.ContentStart)).ToList();
}
表格行
实例在行组
实例中,这些实例在表格
实例中。桌子是块
您可以尝试以下代码:
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();
}
公共静态列表GetSelected段落(流程文档、文本选择)
{
返回文档。块
第()类
.SelectMany(x=>x.RowGroups)
.SelectMany(x=>x.Rows)
.Where(w=>selection.Contains(w.ContentStart))
.ToList();
}
更新:完整示例代码
Xaml
1.
2.
3.
4.
所选行:
选择文本:
代码隐藏
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Documents;
名称空间混合
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
已更改选择上的私有无效(对象发送方、路由目标)
{
TableRowCount.Text=getSelected段落(RichTextBox.Document,RichTextBox.Selection).Count.ToString();
SelectionText.Text=RichTextBox.Selection.Text;
}
公共静态列表GetSelected段落(流文档文档、文本选择)
{
返回文档。块
第()类
.SelectMany(x=>x.RowGroups)
.SelectMany(x=>x.Rows)
.Where(w=>selection.Contains(w.ContentStart))
.ToList();
}
}
}
谢谢filhit,但另一个问题是我无法获得richtextbox选择。m使用TextSelection txtSel=rtxtBox.Selection;但将txtSel.Text设置为空。如何获取richtextbox表的选定行???@AshishBathla我添加了完整示例。这对你有用吗?
<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();
}
}
}