Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all!

I have listbox which shows datacollection via datatemplate (I present each data as customized button). I need to iterract listbox items with keyboard and do Enter press event. Problem is that listbox does not iterract enter press at all. If I choose listbox item by mouse and the press enter it works.

Here is simplified snippet of my problem which shows exact same behaviour.

C#
<ListBox>
         <ListBoxItem>
             <Button Content="ClicMe1" Click="Button_Click"></Button>
         </ListBoxItem>
         <ListBoxItem>
             <Button Content="ClicMe2" Click="Button_Click"></Button>
         </ListBoxItem>
         <ListBoxItem>
             <Button Content="ClicMe3" Click="Button_Click"></Button>
         </ListBoxItem>
     </ListBox>


and codebehind

C#
private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("event received");
        }       


Mouseclick works but keyboard navigation to button and enter press wont, unless I choose it by mouse first.

Hope you got idea :)

UPDATE:

OK, just like Wayne Gaylard suggested I get wanted behaviour when I press twice tab key before I can access to button. This is unfortunate because im sure that end user wont figure it out and mouse click is not option in this case.

Now im trying some automation in SelectionChanged event to focus to button automatically but so far no success. Advices appreciated :)!

Cheers!
Posted
Updated 15-Sep-11 3:59am
v2

You need to press the Tab key twice. First to select the ListBoxItem, then to select the button in the ListBoxItem. Then if you press enter, it will work as expected.
 
Share this answer
 
Comments
paleGegg0 15-Sep-11 9:39am    
Thanks for your time, is there any change to do that code behind? Maybe in selection changed event. I'm quite sure that end user wont be aware tab typing thing...
I think I figured out some workaround. I made following style

C#
<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
    <Setter Property="Focusable" Value="False"/>
</Style>


keyboard navigation seems to work now, only odd behaviour is now that when you set selected index / item from code behind listbox selecteditem highligths again something else than my button but I think I wont do codebehind selections.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900