WPF 文本框焦点

问题描述 投票:0回答:1

我将焦点设置在这样的文本框上:

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

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

wpf focus .net
1个回答
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>

查看这个问题,了解

UserControl
的情况。

© www.soinside.com 2019 - 2024. All rights reserved.