Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Validation.HasError上以WPF MVVM方式将焦点设置为UI控件_Mvvm_Data Binding_Wpf Controls_Wpf 4.5 - Fatal编程技术网

在Validation.HasError上以WPF MVVM方式将焦点设置为UI控件

在Validation.HasError上以WPF MVVM方式将焦点设置为UI控件,mvvm,data-binding,wpf-controls,wpf-4.5,Mvvm,Data Binding,Wpf Controls,Wpf 4.5,问题:Validation.HasError通过INotifyDataErrorInfo实现自动突出显示有错误的控件 我的问题是,当特定控件出错时,我需要将焦点设置在该控件上 如何做到这一点?我已经阅读了Stackoverflow和其他网站上的几篇文章,最后我希望解决这个问题 <Style TargetType="TextBox" > <Setter Property="OverridesDefaultStyle" Valu

问题:Validation.HasError通过INotifyDataErrorInfo实现自动突出显示有错误的控件

我的问题是,当特定控件出错时,我需要将焦点设置在该控件上


如何做到这一点?

我已经阅读了Stackoverflow和其他网站上的几篇文章,最后我希望解决这个问题

   <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中使用布尔属性设置焦点。

Wpf MVVM使用FocusExtension行为

 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();

        }


    }
}
Xaml代码

            <TextBox
                    behavior:FocusExtension.IsFocused="{Binding NameFocus}"
                    Text="{Binding Customer_Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
                    x:Name="txtname" 
                    CharacterCasing="Upper"
                    Grid.Column="3"
                    Grid.Row="1"
                    TextWrapping="Wrap" 
                    BorderThickness="1,1,1,0.5"
                    >

            </TextBox>
设置加载事件

NameFocus=true
NameFocus=true