问题:Validation.HasError通过INotifyDataErrorInfo实现自动突出显示具有Error的控件。
我的问题是我需要在具有ERror时将焦点设置在该特定控件上。
我该怎么做?
答案 0 :(得分:0)
我在Stackoverflow和其他网站上经历了几篇文章,我最后希望解决这个问题。
class String
def dasherize
ActiveSupport::Inflector.dasherize(self)
end
end
设置FocusedElement就可以了。 :) 这也可以用于通过DataTrigger在ViewModel中使用布尔属性来设置焦点而不是简单的触发器。
答案 1 :(得分:0)
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>
视图模型中的MVVM属性
public const string NameFocusPropertyName = "NameFocus";
private bool _NameFocus = default(bool);
public bool NameFocus
{
get
{
return _NameFocus;
}
set
{
if (_NameFocus == value)
{
return;
}
_NameFocus = value;
RaisePropertyChanged(NameFocusPropertyName);
}
}
设置加载事件
NameFocus=true