在我的 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>
我应该怎么做才能在 TextBox 周围获得红色边框?