Skip to main content

Answered Question AutoCompleteBox OnKeyDown event RSS Feed

(0)

derektu123
derektu123

Member

Member

0 points

2 Posts

AutoCompleteBox OnKeyDown event

I am using the AutoCompleteBox in Nov release, and I need to trigger my action after user enter "Enter" key in the textbox area. Is there any way to do that ? I hook up OnKeyDown event, but ACB does not send me Enter key. Thanks in advance.

jwilcox
jwilcox

Participant

Participant

1086 points

190 Posts

Microsoft

Re: AutoCompleteBox OnKeyDown event

Are you interested in a quick-and-dirty solution? Would you be OK re-templating? I can think of a few solutions and post whichever one works the quickest for me...

-Jeff

This posting is provided "AS IS" with no warranties, and confers no rights.

jwilcox
jwilcox

Participant

Participant

1086 points

190 Posts

Microsoft

Re: AutoCompleteBox OnKeyDown event

So, I was just able to successfully retemplate (using the regular template) and insert in XAML my own event for the TextBox's KeyDown.

Here's the XAML: (see: line 16)

1    <UserControl x:Class="SilverlightApplication1.Page"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3                 xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
4        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5        Width="400" Height="300">
6        <Grid x:Name="LayoutRoot" Background="White">
7            <controls:AutoCompleteBox Height="32" x:Name="EnterKeyControl">
8                <controls:AutoCompleteBox.Style>
9                    <Style TargetType="controls:AutoCompleteBox">
10                       <Setter Property="IsTabStop" Value="False" />
11                       <Setter Property="Template">
12                           <Setter.Value>
13                               <ControlTemplate TargetType="controls:AutoCompleteBox">
14                                   <Grid Margin="{TemplateBinding Padding}"
15                             Background="{TemplateBinding Background}">
16                                       <TextBox IsTabStop="True" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" Margin="0"
17                                                 KeyDown="Text_KeyDown"
18                                                />
19                                       <Popup x:Name="Popup">
20                                           <Border x:Name="PopupBorder" HorizontalAlignment="Stretch" Opacity="0.0" BorderThickness="0" CornerRadius="3">
21                                               <Border.RenderTransform>
22                                                   <TranslateTransform X="1" Y="1" />
23                                               </Border.RenderTransform>
24                                               <Border.Background>
25                                                   <SolidColorBrush Color="#11000000" />
26                                               </Border.Background>
27                                               <Border
28                                       HorizontalAlignment="Stretch"
29                                       Opacity="1.0"
30                                       Padding="0"
31                                       BorderThickness="{TemplateBinding BorderThickness}"
32                                       BorderBrush="{TemplateBinding BorderBrush}"
33                                       CornerRadius="3">
34                                                   <Border.RenderTransform>
35                                                       <TransformGroup>
36                                                           <TranslateTransform X="-1" Y="-1" />
37                                                       </TransformGroup>
38                                                   </Border.RenderTransform>
39                                                   <Border.Background>
40                                                       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
41                                                           <GradientStop Color="#FFDDDDDD" Offset="0"/>
42                                                           <GradientStop Color="#AADDDDDD" Offset="1"/>
43                                                       </LinearGradientBrush>
44                                                   </Border.Background>
45                                                   <ListBox
46                                               x:Name="SelectionAdapter"
47                                               ScrollViewer.HorizontalScrollBarVisibility="Auto"
48                                               ScrollViewer.VerticalScrollBarVisibility="Auto"
49                                               ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
50                                               ItemTemplate="{TemplateBinding ItemTemplate}" />
51                                               </Border>
52                                           </Border>
53                                       </Popup>
54                                       <VisualStateManager.VisualStateGroups>
55                                           <VisualStateGroup x:Name="PopupStates">
56                                               <VisualStateGroup.Transitions>
57                                                   <VisualTransition GeneratedDuration="0:0:0.1" To="PopupOpened" />
58                                                   <VisualTransition GeneratedDuration="0:0:0.2" To="PopupClosed" />
59                                               </VisualStateGroup.Transitions>
60                                               <VisualState x:Name="PopupOpened">
61                                                   <Storyboard>
62                                                       <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="1.0" />
63                                                   </Storyboard>
64                                               </VisualState>
65                                               <VisualState x:Name="PopupClosed">
66                                                   <Storyboard>
67                                                       <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="0.0" />
68                                                   </Storyboard>
69                                               </VisualState>
70                                           </VisualStateGroup>
71                                       </VisualStateManager.VisualStateGroups>
72                                   </Grid>
73                               </ControlTemplate>
74                           </Setter.Value>
75                       </Setter>
76                   </Style>
77               </controls:AutoCompleteBox.Style>
78           </controls:AutoCompleteBox>
79       </Grid>
80   </UserControl>
81  

And now, here's what is added to the .cs file:

 

        private void Text_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // set a breakpoint here
            }
        }
 

Just make sure to not set Handled to true... or else the AutoCompleteBox won't be able to complete the commit itself.

With this solution, I believe that Silverlight will call -your- event handler before my AutoCompleteBox handler - so you may need to use a dispatcher invoke or other method to effectively schedule your code, but allow AutoComplete to update the selected value and other parameters first.

Let me know if this helps!

This posting is provided "AS IS" with no warranties, and confers no rights.

derektu123
derektu123

Member

Member

0 points

2 Posts

Re: AutoCompleteBox OnKeyDown event

This works !!

Thanks very much.

However, IMHO, should ACB provide a way for user to customize this behavior without retemplating ?

jwilcox
jwilcox

Participant

Participant

1086 points

190 Posts

Microsoft

Re: AutoCompleteBox OnKeyDown event

Yeah, I agree that would be nice functionality.

One thing that we need to develop is a better validation and forms editing story for Silverlight - ideally AutoCompleteBox would plug in to any such infrastructure. Today, me providing Commit/Cancel events would get the job done, but be specific to the control and not generalizable to other controls. So, this story needs to developer.

I invite you to vote on this, it's an issue that someone posted as a work item in the Silverlight Toolkit site: http://www.codeplex.com/Silverlight/WorkItem/View.aspx?WorkItemId=922 .

-Jeff

This posting is provided "AS IS" with no warranties, and confers no rights.

hehrsson
hehrsson

Member

Member

26 points

72 Posts

Re: AutoCompleteBox OnKeyDown event

What if I want the Enter key to behave as the Tab key?

I´m building a registration form where the user wants to use the Enter key to move to the next field instead of Tab.
If I´m catching the event as you specified above, how do I change it to the Tab key, the e.Key is read only so I can not do e.Key = Key.Tab.

- Håkan

 

jwilcox
jwilcox

Participant

Participant

1086 points

190 Posts

Microsoft
Answered Question

Re: AutoCompleteBox OnKeyDown event

The Tab key is handled by Silverlight's tab navigation service, you'll instead need to try and set focus to the next control. If the next control is named TextBox2, just throw in 'TextBox2.Focus()' in your key handler.

Without writing some painful code to poke around the page, you will need to hard code the focus call. It'll get you going easily.

This posting is provided "AS IS" with no warranties, and confers no rights.

hehrsson
hehrsson

Member

Member

26 points

72 Posts

Re: AutoCompleteBox OnKeyDown event

Oh man!

In my whole invoice registration form including the head and the invoice rows in a DataGrid must the Enter key act as Tab.
We are talking about over 30 fields here.

Is there any way to get the next field in the tab order instead of hard coding the KeyDown event for all controls?
I already have overloaded classes which implement a special interface of all controls I use to get a yellow background when they are on focus.
Maybe I could get this code in here somehow if there is a way to get next control in tab order.

- Håkan

jwilcox
jwilcox

Participant

Participant

1086 points

190 Posts

Microsoft
Answered Question

Re: AutoCompleteBox OnKeyDown event

Well, like I said, you can walk the tree and do that work. Jump up to the parent of the event, walk everything to find the next tab index'd item, and call Focus.

This posting is provided "AS IS" with no warranties, and confers no rights.

cyberharsh
cyberharsh

Member

Member

22 points

7 Posts

Re: AutoCompleteBox OnKeyDown event

derektu123:
I am using the AutoCompleteBox in Nov release, and I need to trigger my action after user enter "Enter" key in the textbox area. Is there any way to do that ? I hook up OnKeyDown event, but ACB does not send me Enter key. Thanks in advance.

Try using KeyUp event instead of KeyDown.. It works for Enter key.

Harshwardhan Joshi,
S/W Developer,
Indigo Architects

Harshwardhan Joshi
Software Developer,
Indigo Architects.

hehrsson
hehrsson

Member

Member

26 points

72 Posts

Re: AutoCompleteBox OnKeyDown event

Thanks, KeyUp solved it for me.

- Håkan

krishna716
krishna716

Member

Member

20 points

11 Posts

Re: AutoCompleteBox OnKeyDown event

KeyUp is solved my issue.

 Thank you.

-Krishna

  • Unanswered Question
  • Answered Question
  • Announcement