如何从WPF中的FlowDocument表中的表行获取表格单元格值?

时间:2013-03-22 13:46:22

标签: c# wpf mouseevent flowdocument tablerow

我希望在TableRow点击行时获取WPF的所有数据。

currentRow = tab.RowGroups[0].Rows[r];
currentRow.MouseLeftButtonDown += new MouseButtonEventHandler(test);

void test(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
   try 
   {
      TableRow tr = sender as TableRow;
      // After that what i do to read TableCell Value
   } 

   catch(Exception ex) 
   {
       MessageBox.Show(ex.Message);
   }
}

请帮忙......

1 个答案:

答案 0 :(得分:1)

我知道它有点晚了,你可能还没有留在这里,更不用说仍然在这个项目上了,但对于那些将来可能会看到这个的人来说:要从每个单元格中获取数据,之后

TableRow tr = sender as TableRow;

执行以下操作:

// I imagine you'd want to start a list here
// that will hold the contents of your loops' results.
List<string> resultsList = new List<string>();
foreach(var tableCell in tr.Cells)
{
    // May want to start another list here in case there are multiple blocks.
    List<string> blockContent = new List<string>();
    foreach(var block in tableCell.Blocks)
    {
        // Probably want to start another list here to which to add in the next loop.
        List<string> inlineContent = new List<string>();
        foreach(var inline in block.Inlines)
        {
            // Implement whatever in here depending the type of inline,
            // such as Span, Run, InlineUIContainer, etc.
            // I just assumed it was text.
            inlineContent.Add(new TextRange(inline.ContentStart, inline.ContentEnd).Text);
        }
        blockContent.Add(string.Join("", inlineContent.ToArray()));
    }
    resultsList.Add(string.Join("\n", blockContent.ToArray()));
}

阅读FlowDocument层次结构可能是个好主意。一个体面的起点是MSDN's Documentation