sipsorcery
sipsorcery

Reputation: 30699

WPF No Events from Controls in 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.

Upvotes: 2

Views: 2228

Answers (2)

Tab
Tab

Reputation: 1762

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**

Upvotes: 0

sipsorcery
sipsorcery

Reputation: 30699

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>

Upvotes: 1

Related Questions