在 WPF 中的 UserControl 中的控件上显示验证错误模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14965110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show Validation Error Template on Controls within a UserControl in WPF
提问by CoderDawson
How do you get the WPF error template to appear on a control within a UserControl in WPF?
如何让 WPF 错误模板出现在 WPF 中 UserControl 内的控件上?
I have a UserControl containing two Labels, two TextBoxes, and a CheckBox. One of the TextBoxes represents the name of the entity and it is bound to a Name property off of a Model property exposed by my ViewModel, which is the DataContext of my Window. The Model class implements the IDataErrorInfo interface and I have confirmed through Unit Testing that when the Name is blank an error is returned through the property indexer implementation. I have bound to the Dependency Property backing the Name TextBox in my UserControl and when the validation error is encountered the WPF error template places a red border around the entire UserControl rather than just the Name TextBox.
我有一个包含两个标签、两个文本框和一个复选框的 UserControl。其中一个 TextBoxes 表示实体的名称,它绑定到我的 ViewModel 公开的 Model 属性的 Name 属性,这是我的 Window 的 DataContext。Model 类实现了 IDataErrorInfo 接口,我已经通过单元测试确认,当 Name 为空时,将通过属性索引器实现返回错误。我已绑定到支持 UserControl 中的 Name TextBox 的 Dependency Property,当遇到验证错误时,WPF 错误模板会在整个 UserControl 周围放置一个红色边框,而不仅仅是 Name TextBox。
The binding to the name field of the UserControl is as follows.
UserControl 的 name 字段的绑定如下。
<vc:MyUserControl ItemName="{Binding Model.Name, ValidatesOnDataErrors=True}" />
A simiplified version of my UserControl and the backing DependencyProperty is as follows.
我的 UserControl 和支持 DependencyProperty 的简化版本如下。
<UserControl>
<Grid>
<TextBox Text="{Binding ItemName}" />
</Grid>
</UserControl>
public partial class MyUserControl: UserControl
{
public static readonly DependencyProperty ItemNameProperty =
DependencyProperty.Register(
"ItemName",
typeof(string),
typeof(MyUserControl),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
);
public string ItemName
{
get { return (string)GetValue(ItemNameProperty); }
set { SetValue(ItemNameProperty, value); }
}
}
The information I have found relating to this issue thus far has all been in regards to Silverlight or using a converter to not show the red border (which did not make sense to me). This information was all found here on stackoverflow.
到目前为止,我发现的与此问题相关的信息都与 Silverlight 或使用转换器不显示红色边框有关(这对我来说没有意义)。这些信息都可以在 stackoverflow 上找到。
Has anyone been able to solve this issue with WPF? Am I overlooking something obvious?
有没有人能够用 WPF 解决这个问题?我是否忽略了一些明显的东西?
回答by LPL
The ErrorTemplatefor UserControlwill be used if bindings to your UserControluse ValidatesOnDataErrors=True. But you can remove the red border with the Validation.ErrorTemplate Attached Property.
将ErrorTemplate用于UserControl将绑定是否可用于您的UserControl使用ValidatesOnDataErrors=True。但是您可以使用Validation.ErrorTemplate Attached Property删除红色边框。
All controls within your UserControlwill only show a red border if you validate their bindings by implementing IDataErrorInfofor the backing DependencyProperties too.
UserControl如果您也通过实现IDataErrorInfo支持 DependencyProperties 来验证它们的绑定,则您中的所有控件只会显示红色边框。
public class MyUserControl : UserControl, IDataErrorInfo
{
public static readonly DependencyProperty ItemNameProperty =
DependencyProperty.Register(
"ItemName",
typeof(string),
typeof(MyUserControl),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
);
public string ItemName
{
get { return (string)GetValue(ItemNameProperty); }
set { SetValue(ItemNameProperty, value); }
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
// use a specific validation or ask for UserControl Validation Error
return Validation.GetHasError(this) ? "UserControl has Error" : null;
}
}
}
and here the simplified XAML
这里是简化的 XAML
<UserControl Validation.ErrorTemplate="{x:Null}">
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
<TextBox Text="{Binding ItemName, ValidatesOnDataErrors=True}" />
</Grid>
</UserControl>
Addition
添加
If you want to differentiate between errors you can get the BindingExpressionfor your DependencyProperty and check the HasError Property.
如果您想区分错误,您可以获取DependencyProperty的BindingExpression并检查HasError 属性。
BindingExpression be = BindingOperations.GetBindingExpression(this, ItemNameProperty);
return be != null && be.HasError ? "ItemName has Error" : null;

