- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 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/
我使用 Prism 和 MVVM 创建了一个基本应用程序。到目前为止,它仅由 Shell 和一个 View/ViewModel 组成。 在应用程序加载过程中,我将 View 加载到我的主要区域中并显示
我使用绑定(bind)将 FocusManager.FocusedElement 作为参数传递。
我有如下的 WPF xaml 代码: 如何将 FocusedElement 绑定(bind)到 ViewModel 中的属性? 类似的代码如下: Switch
我有如下的 WPF xaml 代码: 如何将 FocusedElement 绑定(bind)到 ViewModel 中的属性? 类似的代码如下: Switch
我正在尝试将键盘焦点设置为包含在堆栈面板中的文本框。当 IsEditMode 变为 true 时,我希望文本框默认成为焦点。 我试过这个代码:
Keyboard.FocusedElement 给出具有键盘焦点的元素。但是它没有二传手。我想通过代码将此属性设置为另一个元素。有办法吗? 关于 microsoft's site ,它是这样说的:“键
我有几个数据网格的应用程序并导出到 excel 命令,它将焦点数据网格作为命令参数。是否可以将 CommandParameter 绑定(bind)到 FocusManager.FocusedEleme
我想要一个始终绑定(bind)到当前焦点控件的属性。 我见过两种获取当前焦点控件的方法。 一个是使用 Keyboard.FocusedElement。第二个是遍历可视化树。 因为我想知道每次焦点元素发
我有一个自定义 RoutedUICommand MyCommand 通过 ICommand.Execute 执行.顶部窗口有一个绑定(bind)来处理它: 这是此命令的唯一处理程序。我还
我是一名优秀的程序员,十分优秀!