我有一个非常类似的问题:
带有验证的TextBox在选项卡更改中丢失了ErrorTemplate
AdornerDecorator在Window的同一个实例中欺骗,但是当Window被重新加载时,我切换到包含错误TextBox的TabItem时,ErrorTemplate将不再出现。
<Window x:Class="Views.MyWindowView">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TabControl HorizontalAlignment="Stretch"
Height="Auto"
VerticalAlignment="Top"
Width="Auto"
SelectionChanged="TabItemChanged"
Name="MyTabControl">
<!-- Below, AdornerDecorator are added for the following reason:
the Validation.Error cues are painted in the Adorner Layer.
When tabs are switched, that layer is discarded. -->
<!-- The view 1 tab.-->
<TabItem Header="{Resx tab1_Header}"
Name="Tbi1">
<AdornerDecorator>
<vw:MyView1 DataContext="{Binding}"/>
</AdornerDecorator>
</TabItem>
<!-- The view 2 tab.-->
<TabItem Header="{Resx tab2_Header}"
Name="Tbi2">
<AdornerDecorator>
<vw:MyView2 DataContext="{Binding}"/>
</AdornerDecorator>
</TabItem>
</TabControl>..。
我试图在TabControl SelectionChanged上重新启动代码隐藏中的验证,但失败了。
有什么想法吗?
发布于 2016-12-20 20:41:57
把拼图拼图拼凑在一起,
AdornerLayer表示用于呈现装饰器的表面。由于AdornerLayer通常服务于整个视图,而不仅仅是一个控件,一些容器默认实现它们。
装饰品是绑定到UIElement的自定义FrameworkElement。adorned在AdornerLayer中呈现,它是一个呈现面,它总是位于装饰元素或装饰元素集合的顶部。
因此,在本例中,装饰器(红色矩形)绑定到TextBox,但呈现在TextBox顶部的一个层中。
装饰(例如,在验证错误的情况下)是通过调用静态方法GetAdornerLayer来获得要装饰的AdornerLayer对象来完成的。
足够理论
更改TabItems会丢弃AdornerLayer,从而导致不绘制装饰器。2个解决办法:
\DRapp提出的手动方式:
<XAML for MyView1>
<AdornerDecorator>
...
</AdornerDecorator>
</close XAML for MyView1>当然,如果有另一个容器在AdornerDecorator和TextBox之间(在可视树中)实现一个TextBox,那么这不会有任何好处。因此,显式AdornerDecorator需要是包装TextBox的最后一个。
<XAML for MyView1>
<Grid>
...
<GroupBox>
<AdornerDecorator>
<Grid>
...
<TextBox ... />
</Grid>
</AdornerDecorator>
</GroupBox>
</Grid>
</close XAML for MyView1>\第二个解决方案(我更喜欢),每次当ErrorTemplate变得可见时,就重新设置TextBox。在这样做的时候,AdornerLayer的缺失就会被发现和修正。
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsVisible" Value="true">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>发布于 2016-12-19 16:07:37
我在WPF上看到了一些奇怪的事情,并会提出这个建议。根据您引用并试图在这里使用装饰器装饰器包装视图的其他帖子,将装饰器的包装直接移动到"MvView1“XAML页面中。
从你现有的..。
<AdornerDecorator>
<vw:MyView1 DataContext="{Binding}"/>
</AdornerDecorator>换到..。
<vw:MyView1 DataContext="{Binding}"/>和在
<XAML for MyView1>
<AdornerDecorator>
[wrapping is within the view... then the rest of the view...]
</AdornerDecorator>
</close XAML for MyView1>我注意到了类似的情况,比如应用控件的可见性,除非某些东西在一个层次上工作,而在另一个层次上却不起作用,但从未找到确切的模式。
https://stackoverflow.com/questions/41169165
复制相似问题