【问题标题】:WPF No Events from Controls in RichTextBoxWPF 没有来自 RichTextBox 中的控件的事件
【发布时间】:2009-03-19 01:11:46
【问题描述】:

我正在使用 WPF,并且在 RichTextBox 控件中有一个表格。我需要获取表格单元格的背景颜色以更改它获得焦点。我的问题是我无法为 TableCell 触发 GotFocus 或任何其他事件。

<RichTextBox>
    <FlowDocument>
       <Table>
           <Table.Columns> 
              <TableColumn />
           </Table.Columns>
           <TableRowGroup>
               <TableRow>
                   <TableCell GotFocus="SelectionCell_GotFocus">
                       <Paragraph>1</Paragraph>
                   </TableCell>
               </TableRow>
           </TableRowGroup>
       </Table>
    </FlowDocument>
</RichTextBox>

下图显示了 RichTextBox 控件中的表格。我希望能够在用户在表格单元格之间移动时更改背景。

alt text http://img16.imageshack.us/img16/8151/wpftable.png

编辑:经过更多调查,问题不仅限于 RichTextBox 中的表格,RichTextBox 中的任何控件似乎都无法生成事件。我在其中放置了一个按钮,但无法让它触发其 Click 事件。看起来 RichTextBox 屏蔽了所有事件,希望有办法取消屏蔽它们。

【问题讨论】:

    标签: wpf focus richtextbox


    【解决方案1】:

    一半的答案是将 RichTextBox 上的 IsDocumentEnabled 属性设置为 true。这允许根据Embedded UI Elements in RichTextBox 启用其中的控件。不幸的是,这仍然不会触发我需要的事件,即 TableCell 上的 GotFocus,尽管可以通过在单元格中放置一个按钮并单击它来触发事件。这会将 GotFocus 事件从 UI 树中冒泡到 TableCell。我不想在每个单元格中都有一个按钮,但现在是时候寻找替代解决方案了。

    <RichTextBox IsDocumentEnabled="True">
        <FlowDocument>
            <Table>
                <Table.Columns> 
                    <TableColumn />
                </Table.Columns>
                <TableRowGroup>
                    <TableRow>
                        <TableCell GotFocus="SelectionCell_GotFocus">
                            <BlockUIContainer>
                                <Canvas>
                                    <Button Click="Button_Click">
                                        Click
                                    </Button>
                                </Canvas>
                            </BlockUIContainer>
                        </TableCell>
                    </TableRow>
                </TableRowGroup>
            </Table>
        </FlowDocument>
    </RichTextBox>
    

    【讨论】:

      【解决方案2】:

      更新

      我确实找到了以下内容(在:http://www.databaseforum.info/8/504107.aspx)尝试并且它有效:

      ContentElement,Paragraph 的基类之一,顺便说一下,文档中几乎所有内容的基类定义了您要查找的属性和事件。

      在代码中

      void MyCode() 
      {
          Paragraph p = new Paragraph();
          p.MouseEnter += p_MouseEnter;
      }
      
      void p_MouseEnter(object sender, EventArgs e) 
      {
          Paragraph p = (Paragraph)sender;
          p.Background = Brushes.Red;
      }
      
      **In Markup**
      
      <Paragraph MouseEnter="p_MouseEnter" />
      
      **You can also trigger on properties like IsMouseOver in styles**
      

      【讨论】:

        猜你喜欢
        • 2011-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 2011-06-28
        相关资源
        最近更新 更多