gpt4 book ai didi

c# - FocusedElement 没有得到尊重

转载 作者:太空狗 更新时间:2023-10-29 19:59:47 24 4
gpt4 key购买 nike

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

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

我在一个新项目中重新创建了它,我所做的只是安装 prism/prism.unityextensions,设置 shell 和 View ,并将 View 加载到 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>

View 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文档 -

Logical focus pertains to the FocusManager.FocusedElement within a specific focus scope.

因此,它的 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>

关于c# - FocusedElement 没有得到尊重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com