Setting focus to UI control in WPF MVVM Way on Validation.HasError

夙愿已清 提交于 2019-12-08 11:17:00

问题


Problem: Validation.HasError automatically highlights the control that has Error via INotifyDataErrorInfo implementation.

My problem is i need to set focus on that specific control when it has ERror.

How do I do That?


回答1:


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>

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.



来源:https://stackoverflow.com/questions/35228434/setting-focus-to-ui-control-in-wpf-mvvm-way-on-validation-haserror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!