2

I am setting focus on a Textbox like this:

<DockPanel
        Margin="0,0,0,0"
        LastChildFill="True"
        FocusManager.FocusedElement="{Binding ElementName=messengerTextToSend}">
        <ListBox
            x:Name="messengerLabelParticipants"
            DockPanel.Dock="Top" Height="79" Margin="0,1,0,0" Padding="0"
            Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0"
            AllowDrop="True"
            ItemsSource="{Binding Path=involvedUsers}" ItemTemplate="{StaticResource chatParticipants}" Tag="{Binding Path=chatSessionID}"
            Drop="participantList_Drop" DragEnter="participantList_DragEnter" DragLeave="messengerLabelParticipants_DragLeave">
        </ListBox>
        <TextBox
            x:Name="messengerTextToSend"
            Focusable="True"
            Margin="10,0,10,10"
            DockPanel.Dock="Bottom" Height="100"
            Tag="{Binding Path=.}"
            KeyUp="messengerTextToSend_KeyUp"
            Cursor="IBeam"
            Style="{StaticResource messengerTextBoxSendText}"/>
        <ScrollViewer 
            x:Name="messengerScroller" 
            Template="{DynamicResource ScrollViewerControlTemplate1}" 
            ScrollChanged="messengerScroller_ScrollChanged" Loaded="messengerScroller_Loaded"
            Margin="0,10,0,10">
            <ListBox
                x:Name="messengerListMessages"
                Margin="10,0,0,0" Padding="0" 
                Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding Path=messages}" ItemTemplateSelector="{StaticResource messageTemplateSelector}">
            </ListBox>
        </ScrollViewer>
    </DockPanel>

However, when the page load, although the Textbox visually appears to have focus, the cursor is static and I have to manually either click on the Textbox or tab to it in order to start typing. I'm not sure what I'm doing wrong but I've tried every setting, inclduing setting it in the code to get it working. Any assistance would be greatly appreciated.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jezz
  • 21
  • 1
  • 2

1 Answers1

3

Move the FocusManager.FocusedElement command to the Window element.

<Window x:Class="MYClass.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="My Window" 
    FocusManager.FocusedElement="{Binding ElementName=messengerTextToSend}"> 
    Height="400" 
    Width="600">
   <DockPanel>
   </DockPanel>
</Window>

Check out this question for the case of a user control.

Community
  • 1
  • 1
Zamboni
  • 7,897
  • 5
  • 43
  • 52
  • Thanks, the code is within a Usercontrol, within a window. I have tried adding it to the UserControl however this results in the TextBox not being set to focus in any way. – Jezz Jun 03 '10 at 14:05
  • Look into question: 673536 in Stackoverlfow and setting IsFocusScope to true in your DockPanel. FocusManager.IsFocusScope="True" – Zamboni Jun 03 '10 at 14:51
  • needed to create a specific event handler private void messengerTextToSend_Loaded(object sender, RoutedEventArgs e) { messengerTextToSend.Focus(); } now working fine, thanks for ya help. – Jezz Jun 03 '10 at 15:22