开发者

WPF TextBox Focus

开发者 https://www.devze.com 2023-01-01 21:44 出处:网络
I am setting focus on a Textbox like this: <DockPanel Margin=\"0,0,0,0\" LastChildFill=\"True\" FocusManager.FocusedElement=\"{Binding ElementName=messengerTextToSend}\">

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="messenger开发者_如何转开发LabelParticipants"
            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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消