tags:

views:

17

answers:

0

I'm trying to set keyboard focus to a TextBox within a StackPanel (which is initially rendered as Visibility.Collapsed). When a button is clicked, I change the visual state to "ClientLookup" and the following StoryBoard is invoked which makes the panel visible

    <StackPanel x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MatterLookupViewModel}}"  Orientation="Vertical">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="LookupClientStateGroup">
            <VisualState x:Name="ClientLookup">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CtlClientLookupPanel">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="NoClientLookup"/>
        </VisualStateGroup>

Here is the StackPanel whose Visibility property is changed (to Visible) by the StoryBoard. That all works fine (the panel is indeed displayed).

    <StackPanel x:Name="CtlClientLookupPanel" Visibility="Collapsed" Style="{DynamicResource BackgroundGradient}">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,10">
            <Label Content="Client Lookup" FontSize="14" FontWeight="Bold" />
        </StackPanel>
        <StackPanel Height="30" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,10">
            <Label Content="Enter part of the client name:"/>
            <TextBox x:Name="CtlClientSearchText" Text="{Binding Path=ClientSearchText, UpdateSourceTrigger=PropertyChanged}" Width="100" FontSize="14" TabIndex="0" />
            <Button x:Name="CtlLookupClientNameBtn" Content="Lookup" Margin="10,5,0,5" Command="Search" TabIndex="1" IsDefault="True" />
        </StackPanel>

My problem is that I cannot set focus to CtlClientSearchText (the TextBox within the StackPanel) when I change the visual state to ClientLookup. This snippet doesn't work; focusResult will be false.

        VisualStateManager.GoToElementState(LayoutRoot, "ClientLookup", true);
        bool focusResult = CtlClientSearchText.Focus();

However, this does work if I call CtlClientLookupPanel.Visibility = Visible before the call to CtlClientSearchText.Focus. So the visibility change from the StoryBoard isn't "enough" to allow me to set the keyboard focus to one of it's children?

I thought that perhaps it might be due to the timing of the StoryBoard animation, so I even tried to move the Focus() call to an event that fired at the completion of the StoryBoard animation, but that failed the same way.

Incidentally, if I'm forced to change the Visibility of the StackPanel in code, then I also have to change it to collapsed again when I change the state back to "NoClientLookup". Before I ran into this problem and started messing with the StackPanel's Visibility property in code, the StackPanel was alternating from Visible to Collapsed when the states changed from ClientLookup to NoClientLookup very nicely.