Obs*_*nix 7 c# wpf prism focus mvvm
我使用Prism和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>
Run Code Online (Sandbox Code Playgroud)
视图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>
Run Code Online (Sandbox Code Playgroud)
谁能指出我做错了什么?据我所知,FocusManager.FocusedElement="{Binding ElementName=Username}"应该足以设定焦点.
Roh*_*ats 11
根据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.
您可以参考此处使用附加行为设置键盘焦点 - 在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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中使用 -
<UserControl xmlns:behaviors="clr-namespace:Invoices.Client.Wpf.Behaviors">
<Grid behaviors:KeyboardFocus.On="{Binding ElementName=TextBoxToFocus}">
<TextBox x:Name="TextBoxToFocus" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
FocusManager.FocusedElement="{Binding ElementName=Username}" 设置逻辑焦点,但不设置物理焦点。
物理焦点是正常焦点,逻辑焦点是第二个焦点,在wpf 4.0中仍然有点问题。
我建议您使用Keyboard.Focus(this.Username)。
| 归档时间: |
|
| 查看次数: |
3761 次 |
| 最近记录: |