在我的 App.xaml 中,我定义了一个资源Validation.ErrorTemplate
,这取决于动态BorderBrush
资源。我打算定义独特的BorderBrush
在我拥有的每个窗口以及窗口内的不同块内。
<!--validation error template-->
<ControlTemplate x:Key="NonValid">
<Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="2" Margin="5">
<AdornedElementPlaceholder x:Name="ui"/>
</Border>
</ControlTemplate>
这个演示了我的问题(也带有动态画笔资源)
<!--test template-->
<ControlTemplate x:Key="ButtonRes" TargetType="Button">
<Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="2" Background="Khaki">
<ContentPresenter />
</Border>
</ControlTemplate>
现在我使用这些模板的窗口可以解析普通模板的画笔资源,但不能解析Validation.ErrorTemplate
!
<Window x:Class="MyApp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300">
<Window.Resources>
<!-- window overrides resource-->
<SolidColorBrush x:Key="BorderBrush" Color="Blue"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- button can see window resource-->
<Button Template="{StaticResource ButtonRes}"/>
<Grid Grid.Row="1">
<Grid.Resources>
<!-- grid overrides resource-->
<SolidColorBrush x:Key="BorderBrush" Color="Red"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- button can see grid resource-->
<Button Template="{StaticResource ButtonRes}"/>
<!-- errorTemplate CAN SEE window resource-->
<!-- errorTemplate CAN NOT SEE grid resource-->
<TextBox Grid.Row="1" VerticalAlignment="Center" Text="{Binding Name}"
Validation.ErrorTemplate="{StaticResource NonValid}"/>
</Grid>
</Grid>
</Window>
我应该做什么才能得到RED文本框周围的边框?
您所看到的行为完全没问题。其背后的推理:
Validation.ErrorTemplate
被放置在adorner layer
位于窗口中所有其他控件之上的窗口。这就是为什么它无法查看在网格级别定义的资源并解析窗口资源的引用。
如果您想动态解析它,唯一可能的解决方案是在窗口资源中声明它或使用静态分配。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)