FocusedElement不被兑现 [英] FocusedElement is not being honoured

查看:126
本文介绍了FocusedElement不被兑现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Prism& amp;创建了一个基本应用程序. MVVM.到目前为止,它仅由Shell和一个View/ViewModel组成.

I have created a basic application using Prism & MVVM. So far, it only consists of the Shell, and one View/ViewModel.

在应用程序加载期间,我正在将View加载到我的主要区域中,并在屏幕上显示.这可行,但是我无法使视图上的文本框聚焦.它看起来 就像光标在框中一样(尽管它没有闪烁),但是直到我单击文本框时,它才接受文本输入.

During application load, I am loading the View into my main region and this displays on screen. This works, but I cannot get the textbox on the view to focus. It looks like the cursor is in the box (although it's not flashing), but it doesn't accept text input until I click on the textbox.

我已经在一个新项目中重新创建了该项目,我所做的就是安装棱镜/prism.unityextensions,设置外壳和视图,并将视图加载到外壳区域.这两个xaml文件的代码中都没有任何内容.

I've recreated this in a new project, where all I've done is install prism/prism.unityextensions, set up the shell and the view, and loaded the view into the shell region. Neither xaml file has anything in the code behind.

外壳

<Window x:Class="MVVMFocusTest.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://www.codeplex.com/prism"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DockPanel LastChildFill="True">            
            <ContentControl Name="MainRegion" DockPanel.Dock="Top" prism:RegionManager.RegionName="MainRegion" />
        </DockPanel>
    </Grid>
</Window>

View1

<UserControl x:Class="MVVMFocusTest.View1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <Grid FocusManager.FocusedElement="{Binding ElementName=Username}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />               
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Grid.Column="0">Username</Label>
            <TextBox Name="Username" Grid.Row="0" Grid.Column="1" ToolTip="Enter Username" TabIndex="0" />
            <Label Grid.Row="1" Grid.Column="0">Password</Label>
            <PasswordBox Grid.Row="1" Grid.Column="1" Name="LoginPassword" PasswordChar="*" ToolTip="Enter Password" TabIndex="1" />

        </Grid>
    </StackPanel>
</UserControl>

谁能指出我做错了什么?据我所知,FocusManager.FocusedElement="{Binding ElementName=Username}"应该足以设置焦点.

Can anyone point out what I'm doing wrong? As far as I'm aware, the FocusManager.FocusedElement="{Binding ElementName=Username}" should be sufficient to set the focus.

推荐答案

FocusManager.FocusedElement="{Binding ElementName=Username}"设置逻辑焦点,但不设置物理焦点.

FocusManager.FocusedElement="{Binding ElementName=Username}" sets logical focus but not physical focus.

物理焦点是常规焦点,逻辑焦点是第二个焦点,在wpf 4.0中仍然有点问题.

Physical focus is the normal focus, logical focus is kinda a second focus which is still a little bit buggy in wpf 4.0.

我建议您使用Keyboard.Focus(this.Username).

这篇关于FocusedElement不被兑现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆