我的视图中有TextBox
和Button
。
现在我在按钮点击时检查条件,如果条件结果为假,向用户显示消息,然后我必须将光标设置为TextBox
控件。
if (companyref == null)
{
var cs = new Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation();
MessageBox.Show("Company does not exist.", "Error", MessageBoxButton.OK,
MessageBoxImage.Exclamation);
cs.txtCompanyID.Focusable = true;
System.Windows.Input.Keyboard.Focus(cs.txtCompanyID);
}
上面的代码在ViewModel中。
CompanyAssociation
是视图名称。
但是光标没有在TextBox
中设置。
xaml是:
<igEditors:XamTextEditor Name="txtCompanyID"
KeyDown="xamTextEditorAllowOnlyNumeric_KeyDown"
ValueChanged="txtCompanyID_ValueChanged"
Text="{Binding Company.CompanyId,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Width="{Binding ActualWidth, ElementName=border}"
Grid.Column="1" Grid.Row="0"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Margin="0,5,0,0"
IsEnabled="{Binding Path=IsEditable}"/>
<Button Template="{StaticResource buttonTemp1}"
Command="{Binding ContactCommand}"
CommandParameter="searchCompany"
Content="Search"
Width="80"
Grid.Row="0" Grid.Column="2"
VerticalAlignment="Top"
Margin="0"
HorizontalAlignment="Left"
IsEnabled="{Binding Path=IsEditable}"/>
答案 0 :(得分:245)
让我分三部分回答你的问题。
我想知道你的例子中的“cs.txtCompanyID”是什么?它是TextBox控件吗?如果是,那么你走错了路。一般来说,在ViewModel中对UI进行任何引用都不是一个好主意。你可以问“为什么?”但这是在Stackoverflow上发布的另一个问题:)。
追踪Focus问题的最佳方法是...调试.Net源代码。不开玩笑。它多次为我节省了很多时间。要启用.net源代码调试,请参阅Shawn Bruke's博客。
最后,我用来从ViewModel设置焦点的一般方法是附加属性。我写了非常简单的附加属性,可以在任何UIElement上设置。例如,它可以绑定到ViewModel的属性“IsFocused”。这是:
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool) obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof (bool), typeof (FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement) d;
if ((bool) e.NewValue)
{
uie.Focus(); // Don't care about false values.
}
}
}
现在,在您的视图中(在XAML中),您可以将此属性绑定到ViewModel:
<TextBox local:FocusExtension.IsFocused="{Binding IsUserNameFocused}" />
希望这会有所帮助:)。如果它没有提到答案#2。
干杯。
答案 1 :(得分:65)
我知道这个问题现在已被回答了一千次,但我对Anvaka的贡献进行了一些编辑,我认为这会有助于其他有类似问题的人。
首先,我改变了上述附属财产:
public static class FocusExtension
{
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusExtension), new FrameworkPropertyMetadata(IsFocusedChanged){BindsTwoWayByDefault = true});
public static bool? GetIsFocused(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return (bool?)element.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject element, bool? value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(IsFocusedProperty, value);
}
private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)d;
if (e.OldValue == null)
{
fe.GotFocus += FrameworkElement_GotFocus;
fe.LostFocus += FrameworkElement_LostFocus;
}
if (!fe.IsVisible)
{
fe.IsVisibleChanged += new DependencyPropertyChangedEventHandler(fe_IsVisibleChanged);
}
if ((bool)e.NewValue)
{
fe.Focus();
}
}
private static void fe_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)sender;
if (fe.IsVisible && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
{
fe.IsVisibleChanged -= fe_IsVisibleChanged;
fe.Focus();
}
}
private static void FrameworkElement_GotFocus(object sender, RoutedEventArgs e)
{
((FrameworkElement)sender).SetValue(IsFocusedProperty, true);
}
private static void FrameworkElement_LostFocus(object sender, RoutedEventArgs e)
{
((FrameworkElement)sender).SetValue(IsFocusedProperty, false);
}
}
我添加可见性引用的原因是标签。显然,如果您在最初可见选项卡之外的任何其他选项卡上使用附加属性,则在手动聚焦控件之前附加属性不起作用。
另一个障碍是在失去焦点时创建一种更优雅的方法将底层属性重置为false。这就是丢失焦点事件的来源。
<TextBox
Text="{Binding Description}"
FocusExtension.IsFocused="{Binding IsFocused}"/>
如果有更好的方法来处理可见性问题,请告诉我。
注意:感谢Apfelkuacha建议将BindsTwoWayByDefault放入DependencyProperty中。我很久以前在我自己的代码中做过这个,但从未更新过这篇文章。由于此更改,WPF代码中不再需要Mode = TwoWay。
答案 2 :(得分:29)
我认为最好的方法是保持MVVM原理清洁, 所以基本上你必须使用随MVVM Light提供的Messenger类,以下是如何使用它:
在viewmodel(exampleViewModel.cs)中的:编写以下内容
Messenger.Default.Send<string>("focus", "DoFocus");
现在在你的View.cs中(不是XAML,view.xaml.cs)在构造函数中写下以下内容
public MyView()
{
InitializeComponent();
Messenger.Default.Register<string>(this, "DoFocus", doFocus);
}
public void doFocus(string msg)
{
if (msg == "focus")
this.txtcode.Focus();
}
该方法很好,代码较少,维护MVVM标准
答案 3 :(得分:18)
这些都不适合我,但为了其他人的利益,这是我最后根据这里已经提供的一些代码编写的。
用法如下:
<TextBox ... h:FocusBehavior.IsFocused="True"/>
实施如下:
/// <summary>
/// Behavior allowing to put focus on element from the view model in a MVVM implementation.
/// </summary>
public static class FocusBehavior
{
#region Dependency Properties
/// <summary>
/// <c>IsFocused</c> dependency property.
/// </summary>
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool?),
typeof(FocusBehavior), new FrameworkPropertyMetadata(IsFocusedChanged));
/// <summary>
/// Gets the <c>IsFocused</c> property value.
/// </summary>
/// <param name="element">The element.</param>
/// <returns>Value of the <c>IsFocused</c> property or <c>null</c> if not set.</returns>
public static bool? GetIsFocused(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return (bool?)element.GetValue(IsFocusedProperty);
}
/// <summary>
/// Sets the <c>IsFocused</c> property value.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
public static void SetIsFocused(DependencyObject element, bool? value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(IsFocusedProperty, value);
}
#endregion Dependency Properties
#region Event Handlers
/// <summary>
/// Determines whether the value of the dependency property <c>IsFocused</c> has change.
/// </summary>
/// <param name="d">The dependency object.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Ensure it is a FrameworkElement instance.
var fe = d as FrameworkElement;
if (fe != null && e.OldValue == null && e.NewValue != null && (bool)e.NewValue)
{
// Attach to the Loaded event to set the focus there. If we do it here it will
// be overridden by the view rendering the framework element.
fe.Loaded += FrameworkElementLoaded;
}
}
/// <summary>
/// Sets the focus when the framework element is loaded and ready to receive input.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private static void FrameworkElementLoaded(object sender, RoutedEventArgs e)
{
// Ensure it is a FrameworkElement instance.
var fe = sender as FrameworkElement;
if (fe != null)
{
// Remove the event handler registration.
fe.Loaded -= FrameworkElementLoaded;
// Set the focus to the given framework element.
fe.Focus();
// Determine if it is a text box like element.
var tb = fe as TextBoxBase;
if (tb != null)
{
// Select all text to be ready for replacement.
tb.SelectAll();
}
}
}
#endregion Event Handlers
}
答案 4 :(得分:11)
这是一个旧帖子,但似乎没有代码解答Anavanka接受的答案的问题:如果你设置属性,它不起作用viewmodel为false,或者如果将属性设置为true,则用户手动单击其他内容,然后再次将其设置为true。在这些情况下,我无法让Zamotic的解决方案可靠地工作。
将上面的一些讨论结合在一起,给出了下面的代码,我想到了这些问题:
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new UIPropertyMetadata(false, null, OnCoerceValue));
private static object OnCoerceValue(DependencyObject d, object baseValue)
{
if ((bool)baseValue)
((UIElement)d).Focus();
else if (((UIElement) d).IsFocused)
Keyboard.ClearFocus();
return ((bool)baseValue);
}
}
话虽如此,对于可以在代码隐藏中的一行中完成的事情来说仍然很复杂,并且CoerceValue并不是真的意味着以这种方式使用,所以也许代码隐藏是可行的方法。
答案 5 :(得分:4)
就我而言,在我更改方法OnIsFocusedPropertyChanged之前,FocusExtension不起作用。当断点停止进程时,原始的只在调试中工作。在运行时,过程太快,没有任何事情发生。通过这个小小的修改和我们的朋友Task的帮助,这在两种情况下都能正常工作。
private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
var action = new Action(() => uie.Dispatcher.BeginInvoke((Action)(() => uie.Focus())));
Task.Factory.StartNew(action);
}
}
答案 6 :(得分:3)
Anvakas精彩的代码适用于Windows桌面应用程序。如果您像我一样需要与Windows应用商店应用相同的解决方案,则此代码可能很方便:
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new PropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
var uie = d as Windows.UI.Xaml.Controls.Control;
if( uie != null )
{
uie.Focus(FocusState.Programmatic);
}
}
}
}
答案 7 :(得分:2)
问题是,一旦将IsUserNameFocused设置为true,它将永远不会为false。这通过处理FrameworkElement的GotFocus和LostFocus来解决它。
我在使用源代码格式时遇到问题,所以这里是link
答案 8 :(得分:1)
对于那些试图使用上面的Anvaka解决方案的人来说,我第一次遇到绑定问题,因为lostfocus不会将属性更新为false。您可以手动将属性设置为false,然后每次都设置为true,但更好的解决方案可能是在您的属性中执行类似的操作:
bool _isFocused = false;
public bool IsFocused
{
get { return _isFocused ; }
set
{
_isFocused = false;
_isFocused = value;
base.OnPropertyChanged("IsFocused ");
}
}
这样你只需要将它设置为true,它就会得到焦点。
答案 9 :(得分:1)
我使用WPF / Caliburn Micro发现“dfaivre”已经提供了一个通用且可行的解决方案 这里: http://caliburnmicro.codeplex.com/discussions/222892
答案 10 :(得分:1)
我通过编辑代码找到了解决方案,如下所示。没有必要先将Binding属性设置为False,然后设置为True。
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d != null && d is Control)
{
var _Control = d as Control;
if ((bool)e.NewValue)
{
// To set false value to get focus on control. if we don't set value to False then we have to set all binding
//property to first False then True to set focus on control.
OnLostFocus(_Control, null);
_Control.Focus(); // Don't care about false values.
}
}
}
private static void OnLostFocus(object sender, RoutedEventArgs e)
{
if (sender != null && sender is Control)
{
(sender as Control).SetValue(IsFocusedProperty, false);
}
}
}
答案 11 :(得分:0)
对于Silverlight:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace MyProject.Behaviors
{
public class FocusBehavior : Behavior<Control>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
base.OnAttached();
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
if (this.HasInitialFocus || this.IsFocused)
{
this.GotFocus();
}
}
private void GotFocus()
{
this.AssociatedObject.Focus();
if (this.IsSelectAll)
{
if (this.AssociatedObject is TextBox)
{
(this.AssociatedObject as TextBox).SelectAll();
}
else if (this.AssociatedObject is PasswordBox)
{
(this.AssociatedObject as PasswordBox).SelectAll();
}
else if (this.AssociatedObject is RichTextBox)
{
(this.AssociatedObject as RichTextBox).SelectAll();
}
}
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.Register(
"IsFocused",
typeof(bool),
typeof(FocusBehavior),
new PropertyMetadata(false,
(d, e) =>
{
if ((bool)e.NewValue)
{
((FocusBehavior)d).GotFocus();
}
}));
public bool IsFocused
{
get { return (bool)GetValue(IsFocusedProperty); }
set { SetValue(IsFocusedProperty, value); }
}
public static readonly DependencyProperty HasInitialFocusProperty =
DependencyProperty.Register(
"HasInitialFocus",
typeof(bool),
typeof(FocusBehavior),
new PropertyMetadata(false, null));
public bool HasInitialFocus
{
get { return (bool)GetValue(HasInitialFocusProperty); }
set { SetValue(HasInitialFocusProperty, value); }
}
public static readonly DependencyProperty IsSelectAllProperty =
DependencyProperty.Register(
"IsSelectAll",
typeof(bool),
typeof(FocusBehavior),
new PropertyMetadata(false, null));
public bool IsSelectAll
{
get { return (bool)GetValue(IsSelectAllProperty); }
set { SetValue(IsSelectAllProperty, value); }
}
}
}
LoginViewModel.cs:
public class LoginModel : ViewModelBase
{
....
private bool _EmailFocus = false;
public bool EmailFocus
{
get
{
return _EmailFocus;
}
set
{
if (value)
{
_EmailFocus = false;
RaisePropertyChanged("EmailFocus");
}
_EmailFocus = value;
RaisePropertyChanged("EmailFocus");
}
}
...
}
Login.xaml:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:beh="clr-namespace:MyProject.Behaviors"
<TextBox Text="{Binding Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Behaviors>
<beh:FocusBehavior IsFocused="{Binding EmailFocus}" IsSelectAll="True"/>
</i:Interaction.Behaviors>
</TextBox>
OR
<TextBox Text="{Binding Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Behaviors>
<beh:FocusBehavior HasInitialFocus="True" IsSelectAll="True"/>
</i:Interaction.Behaviors>
</TextBox>
设置焦点应该只在代码中执行:
EmailFocus = true;
请记住,此插件是html页面的一部分,因此页面中的其他控件可能具有焦点
if (!Application.Current.IsRunningOutOfBrowser)
{
System.Windows.Browser.HtmlPage.Plugin.Focus();
}
答案 12 :(得分:0)
基于@Sheridan答案here
的另一种方法 <TextBox Text="{Binding SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding SomeTextIsFocused, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
在视图模型中,以通常的方式设置绑定,然后将SomeTextIsFocused设置为true以将焦点设置在文本框上
答案 13 :(得分:0)
实现公认的答案后,我确实遇到了一个问题,即在使用Prism导航视图时,TextBox仍不会获得焦点。对PropertyChanged处理程序进行较小的更改即可解决
private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
uie.Focus();
}));
}
}
答案 14 :(得分:0)
首先,我要感谢Avanka帮助我解决我的焦点问题。但是他发布的代码中存在一个错误,即在行中: if(e.OldValue == null)
我遇到的问题是,如果您先在视图中单击并聚焦控件,则e.oldValue不再为null。然后,当您将变量设置为第一次聚焦控件时,这会导致lostfocus和gotfocus处理程序未被设置。 我对此的解决方案如下:
public static class ExtensionFocus
{
static ExtensionFocus()
{
BoundElements = new List<string>();
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool?),
typeof(ExtensionFocus), new FrameworkPropertyMetadata(false, IsFocusedChanged));
private static List<string> BoundElements;
public static bool? GetIsFocused(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("ExtensionFocus GetIsFocused called with null element");
}
return (bool?)element.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject element, bool? value)
{
if (element == null)
{
throw new ArgumentNullException("ExtensionFocus SetIsFocused called with null element");
}
element.SetValue(IsFocusedProperty, value);
}
private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)d;
// OLD LINE:
// if (e.OldValue == null)
// TWO NEW LINES:
if (BoundElements.Contains(fe.Name) == false)
{
BoundElements.Add(fe.Name);
fe.LostFocus += OnLostFocus;
fe.GotFocus += OnGotFocus;
}
if (!fe.IsVisible)
{
fe.IsVisibleChanged += new DependencyPropertyChangedEventHandler(fe_IsVisibleChanged);
}
if ((bool)e.NewValue)
{
fe.Focus();
}
}
private static void fe_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)sender;
if (fe.IsVisible && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
{
fe.IsVisibleChanged -= fe_IsVisibleChanged;
fe.Focus();
}
}
private static void OnLostFocus(object sender, RoutedEventArgs e)
{
if (sender != null && sender is Control s)
{
s.SetValue(IsFocusedProperty, false);
}
}
private static void OnGotFocus(object sender, RoutedEventArgs e)
{
if (sender != null && sender is Control s)
{
s.SetValue(IsFocusedProperty, true);
}
}
}
答案 15 :(得分:0)
您可以使用 ViewCommand 设计模式。它描述了MVVM设计模式通过命令从ViewModel控制View的方法。
我是根据King A.Majid建议使用MVVM Light Messenger类实现的。 ViewCommandManager类处理在连接视图中调用命令。它基本上是常规命令的另一个方向,对于ViewModel需要在其View中执行某些操作的情况。它使用数据绑定命令和WeakReferences之类的反射来避免内存泄漏。
http://dev.unclassified.de/source/viewcommand(也发布在CodeProject上)
答案 16 :(得分:-1)
public class DummyViewModel : ViewModelBase
{
private bool isfocused= false;
public bool IsFocused
{
get
{
return isfocused;
}
set
{
isfocused= value;
OnPropertyChanged("IsFocused");
}
}
}
答案 17 :(得分:-1)
似乎没有人包含最后一步,以便通过绑定变量更新属性。这就是我想出来的。如果有更好的方法,请告诉我。
XAML
public function validateIdentifier(FormEvent $event)
{
$identifier = $event->getData();
$element = $event->getForm();
if ($identifier) {
if ($this->identifierIsUrl($identifier)) {
$parser = $this->getIdParser();
$identifier = $parser->getIdentifier($identifier);
if (null === $identifier) {
$element->addError(new FormError('You have either entered an incorrect url for the source or it could not be parsed'));
}
}
$event->setData($identifier);
}
}
视图模型
<TextBox x:Name="txtLabel"
Text="{Binding Label}"
local:FocusExtension.IsFocused="{Binding txtLabel_IsFocused, Mode=TwoWay}"
/>
<Button x:Name="butEdit" Content="Edit"
Height="40"
IsEnabled="{Binding butEdit_IsEnabled}"
Command="{Binding cmdCapsuleEdit.Command}"
/>
答案 18 :(得分:-1)
只需执行以下操作:
<Window x:class...
...
...
FocusManager.FocusedElement="{Binding ElementName=myTextBox}"
>
<Grid>
<TextBox Name="myTextBox"/>
...
答案 19 :(得分:-1)
我发现Crucial对IsVisible问题的解决方案非常有用。它没有完全解决我的问题,但是一些额外的代码遵循IsEnabled模式的相同模式。
我添加了IsFocusedChanged方法:
if (!fe.IsEnabled)
{
fe.IsEnabledChanged += fe_IsEnabledChanged;
}
这是处理程序:
private static void fe_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)sender;
if (fe.IsEnabled && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
{
fe.IsEnabledChanged -= fe_IsEnabledChanged;
fe.Focus();
}
}
答案 20 :(得分:-7)
System.Windows.Forms.Application.DoEvents();
Keyboard.Focus(tbxLastName);