设置焦点到WPF MVVM中的UI控件方式Validation.HasError [英] Setting focus to UI control in WPF MVVM Way on Validation.HasError
问题描述
问题:Validation.HasError通过INotifyDataErrorInfo实现自动突出显示有错误的控件。
Problem: Validation.HasError automatically highlights the control that has Error via INotifyDataErrorInfo implementation.
我的问题是我需要将焦点放在具有ERR的具体控件上。
My problem is i need to set focus on that specific control when it has ERror.
我该怎么做?
推荐答案
在Stackoverflow和其他网站上的几篇文章,我终于希望解决这个问题。
I have gone through several articles in Stackoverflow and other sites and i finally wish to address this problem.
<Style TargetType="TextBox" >
<Setter Property="OverridesDefaultStyle" Value="false"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="5,3" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
设置FocusedElement做的伎俩。 :)
这也可以用于通过DataTrigger使用ViewModel中的布尔属性设置焦点,而不是简单的触发器。
Setting FocusedElement did the trick. :) This can also be used to set focus using a boolean property in ViewModel via DataTrigger than a simple trigger.
这篇关于设置焦点到WPF MVVM中的UI控件方式Validation.HasError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!