tags:

views:

285

answers:

2

How in MVVM do you set a User Control Focus? Using Focusmanager.FocusElement={Binding ...} Has no affect.

Here is my XAML:

<DataTemplate DataType="{x:Type client:TelephoneNumberViewModel}">
    <Grid FocusManager.FocusedElement="{Binding ElementName=TelephoneNumber}" Width="1024" Height="540">
        <Grid.RowDefinitions>
            <RowDefinition Height="60" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="80" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Margin="0 25 0 0" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="1" 
             Grid.ColumnSpan="7"  Name="textBlockQuestion"
             TextWrapping="Wrap" Style="{DynamicResource TitleTextBlock}">"What is your telephone number?"</TextBlock>

        <Grid Grid.Column="1" Grid.Row="1" Grid.RowSpan="7" 
             Grid.ColumnSpan="7" Height="460">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="170" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="80" />
                <RowDefinition Height="100" />
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Top" HorizontalAlignment="Left">
                <wpfclient:TelephoneBox Name="TelephoneNumber"  TelephoneNumber="{Binding PhoneNumber, Mode=TwoWay}" />
                <!--<TextBox Width="500" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="40" TextAlignment="Center" Text="{Binding PhoneNumber}"></TextBox>-->

                <StackPanel Orientation="Horizontal" Margin="0 40 0 0">
                    <StackPanel Orientation="Vertical" Margin="0 0 60 0" >
                        <Button Margin="0 0 0 20" Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyMobileCommand}">Mobile</Button>
                        <Button Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyHomeCommand}">Home</Button>

                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <Button Margin="0 0 0 20"  Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyWorkCommand}">Work</Button>
                        <Button Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyOtherCommand}">Other</Button>
                    </StackPanel>
                </StackPanel>



            </StackPanel>


        </Grid>



        <Grid Width="1024" Height="80" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="8" VerticalAlignment="Bottom">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Margin="60 0 0 20" Grid.Column="0" Grid.Row="0"  Name="buttonContinue" 
                    Command="{Binding SurveySkipCommand}" Style="{DynamicResource LargeGlossyButtonStyle}" 
                    >Skip</Button>
        </Grid>



    </Grid>
</DataTemplate>

The user control does not get focus until I tab once still.

+1  A: 

Use FocusManager.FocusedElement.

Edit after your edit: I don't think it's possible to say without seeing your entire code, but I think this might be a focus scoping issue. You're setting the logical focus to the telephone control, but that will only have keyboard focus if that logical scope is the active one. I suspect something outside the control being templated has its own focus scope and it's not until you hit tab that the scope moves into the UserControl.

HTH, Kent

Kent Boogaart
Please see my update. The Telephone box no mater where I place the FocusManager.FocusedElement never gets focus.
cjibo
A: 

What is the context of the focus? Are they tabbing to the control, is the control receiving focus as a new item is added?

If the latter, make sure that the ListBox the DataTemplate is for has IsSynchronizedWithCurrentItem="True"

Jeff Wain
The basis is when the viewmodel is loaded I want the TelephoneBox to be focused for input. We are utilizing Prism so the ViewModel is set into a region.
cjibo
You may need to look at your parent elements and if it is the parent UserCOntrol that has focus, you may want to make sure that Focusable="False" on the parent controls--not sure how Prism declares the parent. You may want to use Mole to see what is getting focus if you can't find it from your code-behind. http://karlshifflett.wordpress.com/mole-for-visual-studio/
Jeff Wain