FocusedElement没有被尊重

时间:2013-11-29 23:23:38

标签: c# wpf mvvm focus prism

我使用Prism& amp;创建了一个基本应用程序。 MVVM。到目前为止,它只包含Shell和一个View / ViewModel。

在应用程序加载期间,我将View加载到我的主区域,并在屏幕上显示。这有效,但我不能让视图上的文本框集中。 看起来就像光标在框中一样(尽管它没有闪烁),但是在我点击文本框之前它不接受文本输入。

我在一个新项目中重新创建了这个,我所做的就是安装prism / prism.unityextensions,设置shell和视图,并将视图加载到shell区域。 xaml文件后面的代码都没有。

贝壳

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

视图1

<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}"应该足以设定焦点。

2 个答案:

答案 0 :(得分:8)

根据FocusManager文档 -

  

逻辑焦点属于一个中的FocusManager.FocusedElement   具体的重点范围。

因此,它的not necessary that element with logical focus will have keyboard focus as well反之亦然,即element with keyboard focus will surely have a logical focus as well.

如文档FocusManager.FocusedElement guarantees logical focus and not keyboard focus中所述。所以你可以做的是创建一个attach behaviour,类似于FocusManager.FocusedElement set keyboard focus on an element

您可以参考此处使用附加行为设置键盘焦点 - Setting keyboard focus in WPF

该文章的代码 -

namespace Invoices.Client.Wpf.Behaviors
{
    using System.Windows;
    using System.Windows.Input;

    public static class KeyboardFocus
    {
        public static readonly DependencyProperty OnProperty;

        public static void SetOn(UIElement element, FrameworkElement value)
        {
            element.SetValue(OnProperty, value);
        }

        public static FrameworkElement GetOn(UIElement element)
        {
            return (FrameworkElement)element.GetValue(OnProperty);
        }

        static KeyboardFocus()
        {
            OnProperty = DependencyProperty.RegisterAttached("On", typeof(FrameworkElement), typeof(KeyboardFocus), new PropertyMetadata(OnSetCallback));
        }

        private static void OnSetCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var frameworkElement = (FrameworkElement)dependencyObject;
            var target = GetOn(frameworkElement);

            if (target == null)
                return;

            frameworkElement.Loaded += (s, e) => Keyboard.Focus(target);
        }
    }
}

在XAML中使用 -

<UserControl xmlns:behaviors="clr-namespace:Invoices.Client.Wpf.Behaviors">
    <Grid behaviors:KeyboardFocus.On="{Binding ElementName=TextBoxToFocus}">
        <TextBox x:Name="TextBoxToFocus" />
    </Grid>
</UserControl>

答案 1 :(得分:5)

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

物理焦点是正常的焦点,逻辑焦点是第二个焦点,在wpf 4.0中仍然有点小问题。

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