【问题标题】:FocusedElement is not being honouredFocusedElement 没有受到尊重
【发布时间】:2013-12-16 04:12:44
【问题描述】:

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

在应用程序加载过程中,我将视图加载到我的主要区域,这会显示在屏幕上。这有效,但我无法让视图上的文本框聚焦。 看起来光标在框中(虽然它没有闪烁),但在我点击文本框之前它不接受文本输入。

我在一个新项目中重新创建了它,我所做的只是安装 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}" 应该足以设置焦点。

【问题讨论】:

  • 我要做的是创建一个虚拟转换器并将其放置在绑定 FocusManager.FocusedElement="{Binding ElementName=Username , Path=. , Converter={StaticResource SomeDummyConverter}}" 现在放置一个断点在转换器的转换函数中并观察您的 TextBox 元素,它可能在您设置焦点时被禁用 (IsEnabled = False)。

标签: c# wpf mvvm focus prism


【解决方案1】:

根据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 中所述。所以你可以做的是创建一个类似于FocusManager.FocusedElementattach behaviour,这将是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>

【讨论】:

  • 首选方法,IMO。将其保存在 XAML 中。
  • 这太棒了。我还将目标检索和 Keyboard.Focus(target) 提取到“OnLoaded”方法并添加 textBox.CaretIndex = textBox.Text.Length 以改进更新时的功能。
【解决方案2】:

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

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

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

【讨论】:

  • 我不知道你在说什么,如果你要声称有人发布了错误的东西,总是写下为什么会这样。我的回答没有错。我会为你引用msdn Eran。 “FocusedElement 是对特定焦点范围具有逻辑焦点的元素。此对象可能具有也可能没有键盘焦点。键盘焦点是指接收键盘输入的元素。” msdn.microsoft.com/en-us/library/…(看有逻辑焦点的部分,逻辑焦点不是真正的焦点)
  • 将其添加到视图代码隐藏中的 Loaded 事件时有效。谢谢。但是,有没有办法通过 xaml 中的标记来做到这一点?我可以在代码隐藏中忍受它,但如果可能的话,我宁愿避免它。
  • @ObsidianPhoenix 即使使用 FocusManager 也有方法。没有任何附加的属性或代码。我不知道你遇到了什么样的情况,所以我建议你使用 Keyboard.Focus(..) 方法,但有些情况下 focusmanager 也可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多