WPF TextBox焦点

时间:2010-06-03 13:05:36

标签: wpf focus .net

我将重点放在像这样的文本框上:

<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>

然而,当页面加载时,虽然文本框在视觉上看起来有焦点,但光标是静态的,我必须手动点击文本框或选项卡才能开始输入。我不确定我做错了什么,但我已经尝试了所有设置,包括在代码中设置它以使其正常工作。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

将FocusManager.FocusedElement命令移动到Window元素。

<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>

对于用户控件的情况,请查看此question