隐藏控件时隐藏验证装饰 [英] Hiding validation adornment when hiding a control
问题描述
在WPF中,如何隐藏控件时如何隐藏验证错误模板装饰(默认为红色框)?当我隐藏控件(以方便在视图之间切换)时,错误装饰仍然存在。
How, in WPF, do you hide the validation error template adornment (red box by default) when you hide a control? When I hide my controls (to facilitate switching between views) the error adornment sticks around.
更困难的是,如何使用MVVM做到这一点?
Even more difficult, how do I do this using MVVM?
推荐答案
Validation.ErrorTemplate $的默认
ControlTemplate
c $ c>具有 AdornedElementPlaceholder
,而该引用又引用了其 AdornedElement
。看起来像这样
The default ControlTemplate
for the Validation.ErrorTemplate
has an AdornedElementPlaceholder
which in turn has a reference to its AdornedElement
. It looks like this
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
从这里可以绑定 Border $ c $的可见性c>到
AdornedElementPlaceholder.AdornedElement
的可见性以链接其可见性。然后,使所有出现此问题的 Control
都使用此 Validation.ErrorTemplate
而不是默认值。这是一个示例
From here would could bind the Visibility of the Border
to the Visibility of the AdornedElementPlaceholder.AdornedElement
to link their Visibility. Then we make all the Control
's that has this problem use this Validation.ErrorTemplate
instead of the default one. Here's an example
Xaml
<Window.Resources>
<ControlTemplate x:Key="ValidationErrorTamplate">
<Border Visibility="{Binding ElementName=placeHolder,
Path=AdornedElement.Visibility}"
BorderBrush="Red"
BorderThickness="1">
<AdornedElementPlaceholder x:Name="placeHolder"/>
</Border>
</ControlTemplate>
</Window.Resources>
<TextBox ...
Validation.ErrorTemplate="{StaticResource ValidationErrorTamplate}">
更新
引用父级 UserControl
在绑定中您可以
1。对于特定控件,您可以使用父项
属性
1.For a specific control you can walk up the logical tree using the Parent
Property
示例:如果 TextBox
位于 UserControl
中的 StackPanel
中,我们可以使用Parent.Parent
Example: If the TextBox
is located in a StackPanel
in the UserControl
we can reference it with Parent.Parent
<UserControl ...>
<StackPanel>
<TextBox ...
Validation.ErrorTemplate="{StaticResource ValidationErrorTamplate2}">
<ControlTemplate x:Key="ValidationErrorTamplate2">
<Border Visibility="{Binding ElementName=placeHolder,
Path=AdornedElement.Parent.Parent.Visibility}"
BorderBrush="Red"
BorderThickness="1">
<AdornedElementPlaceholder x:Name="placeHolder"/>
</Border>
</ControlTemplate>
2。要获得更动态的方法,可以使用 ResourceDictionary
,并在文件后面添加代码,您可以在其中使用 Border
的Loaded事件。在其中,您可以走到可视树上,找到父 UserControl
并将其用作绑定的源
2.For a more dynamic approach you can use a ResourceDictionary
with a code behind file where you make use of the Loaded event for the Border
. In it, you walk up the visual tree to find the parent UserControl
and use that as the source for the Binding
ValidationErrorTemplateDictionary.xaml
<ResourceDictionary x:Class="ValidationErrorVisibility.ValidationErrorTemplateDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="ValidationErrorTamplate3">
<Border BorderBrush="Red"
BorderThickness="1"
Loaded="ValidationAdorner_Loaded">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</ResourceDictionary>
ValidationErrorTemplateDictionary.xaml.cs
public partial class ValidationErrorTemplateDictionary
{
private void ValidationAdorner_Loaded(object sender, RoutedEventArgs e)
{
Border adornedBorder = sender as Border;
Binding visibilityBinding = new Binding("Visibility");
UIElement adornedElement = ((AdornedElementPlaceholder)adornedBorder.Child).AdornedElement;
UserControl parentUserControl = GetVisualParent<UserControl>(adornedElement);
visibilityBinding.Source = parentUserControl;
adornedBorder.SetBinding(Border.VisibilityProperty, visibilityBinding);
}
public static T GetVisualParent<T>(object childObject) where T : Visual
{
DependencyObject child = childObject as DependencyObject;
while ((child != null) && !(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return child as T;
}
}
您的UserControl
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ValidationErrorTemplateDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel>
<TextBox ...
Validation.ErrorTemplate="{StaticResource ValidationErrorTamplate3}">
这篇关于隐藏控件时隐藏验证装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!