WPF RichTextBox 中的控件没有事件

发布于 2024-07-15 02:49:22 字数 1055 浏览 6 评论 0 原文

我正在使用 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 控件中的表格。 我希望能够做的是当用户在表格单元格之间移动时更改背景。

替代文本 http://img16.imageshack.us/img16/8151/wpftable.png< /a>

编辑:经过更多调查后,问题不仅限于 RichTextBox 中的表,RichTextBox 中的任何控件似乎都无法生成事件。 我在其中放置了一个按钮,但无法让它触发其 Click 事件。 看起来 RichTextBox 掩盖了所有事件,希望有一种方法可以揭开它们。

I am using WPF and have a Table inside a RichTextBox control. I need to get the background colour of the table cell to change it gets the focus. My problem is that I can't get the GotFocus or any other events to fire for the TableCell.

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

The image below shows the table in the RichTextBox control. What I'd like to be able to do is change the background as the user moves between the table cells.

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

Edit: After more investigation the issue is not confined to Table's in a RichTextBox, no control in a RichTextBox appears to be able to generate events. I placed a button into it and was not bale to get it to fire its Click event. It looks like the RichTextBox masks all events, hopefully there is a way to unmask them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-07-22 02:49:22

一半的答案是将 RichTextBox 上的 IsDocumentEnabled 属性设置为 true。 这允许按照 RichTextBox 中嵌入的 UI 元素。 不幸的是,这仍然没有触发我需要的事件,即 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>

The half answer is to set the IsDocumentEnabled property on the RichTextBox to true. That allows controls within it to be enabled as per Embedded UI Elements in RichTextBox. Unfortunately that still doesn't fire the event I need which is the GotFocus on a TableCell although it is possible to get the event to fire by putting a button in the cell and clicking on it. That bubbles the GotFocus event up the UI tree to the TableCell. I don't want a button in every cell though so time to look for an alternative solution.

<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>
梦开始←不甜 2024-07-22 02:49:22

更新

我确实找到了以下内容(位于: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**

Update

I did find the following (at: http://www.databaseforum.info/8/504107.aspx) TRIED AND IT WORKS:

ContentElement, one of the base classes of Paragraph and incidentally the base class of just about everything that lives in a document defines the properties and events you are looking for.

In Code

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**
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文