locked
Why doesn't ContentPresenter.KeyDown event get fired? RRS feed

  • Question

  • The KeyDown event isn't getting fired in the following code snippet (in either location).

    <sdk:TreeView ItemsSource="{Binding Criteria.Criterions}">
        <sdk:TreeView.ItemTemplate>
            <sdk:HierarchicalDataTemplate ItemsSource="{Binding ShownItems}">
                <ContentPresenter Content="{Binding FieldLabel}"
                                    KeyDown="ContentPresenter_KeyDown" />
                <sdk:HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <ContentPresenter Content="{Binding Value}" 
                                            KeyDown="ContentPresenter_KeyDown" />
                    </DataTemplate>
                </sdk:HierarchicalDataTemplate.ItemTemplate>
            </sdk:HierarchicalDataTemplate>
        </sdk:TreeView.ItemTemplate>
    </sdk:TreeView>

    Please enlighten me as to why this event doesn't get fired.

    Thanks!


    Friday, February 11, 2011 11:33 AM

Answers

  • Hi,

    In order to generate the KeyDown event, the UIElement must have focus

    As I know, ContentPresenter, TextBlock, Border could not get focus, so the Key events could not work.

    So I suggest to use a Button instead of the ContentPresenter.

    Tuesday, February 15, 2011 3:52 AM

All replies

  • Did you try the KeyUp event?

    Friday, February 11, 2011 11:34 AM
  • Yeah, I just tried KeyUp.  It's not getting fired either.

    Friday, February 11, 2011 11:45 AM
  • What are you using for the "Content"?  Does the Content have a Background to capture the hit test?

    Friday, February 11, 2011 11:46 AM
  • I am merely trying to better understand the TreeView control so that I will be able to utilize it more effectively.

    I have tried substituting a TextBlock for the ContentPresenter.  But the TextBlock didn't fire the Key[Down|Up] events either.  And I also tried wrapping the ContentPresenter with a Border and hooking up the Border's Key[Down|Up] events.  These events weren't fired either.

    Friday, February 11, 2011 12:23 PM
  • Hi,

    In order to generate the KeyDown event, the UIElement must have focus

    As I know, ContentPresenter, TextBlock, Border could not get focus, so the Key events could not work.

    So I suggest to use a Button instead of the ContentPresenter.

    Tuesday, February 15, 2011 3:52 AM