将 ControlTemplate 应用到窗口时错误模板不起作用

1 c# wpf wpf-controls controltemplate errortemplate

我有一个带有 App.xaml、MainWindow.xaml 和 Person 类的简单应用程序。当我不指定模板时,我的 ValidateOnDataErrors 工作正常,当文本框出错时,会在文本框周围放置红色边框。但是,一旦我在 MainWindow.xaml 的 Window 标记中插入“Template="{StaticResource WindowTemplate}"”,该字段仍然会被验证,但红色边框就会消失。

应用程序.xaml:

<Application x:Class="WpfPOC.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ControlTemplate TargetType="Window" x:Key="WindowTemplate">
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Control x:Name="FocusCatcher"></Control>
            <TextBlock>Menu Section</TextBlock>
            <ContentPresenter Grid.Row="1" />
            <StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
                <TextBlock Text="Current Editing Mode" />
            </StatusBar>
        </Grid>
    </ControlTemplate>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml:

<Window x:Class="WpfPOC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:WpfPOC"
    Template="{StaticResource WindowTemplate}"
    Title="MainWindow" 
    Height="350" 
    Width="525">
<Window.Resources>
    <data:Person x:Key="myDataSource" Name="Joe"/>
</Window.Resources>
<Grid>
    <TextBox Height="23" Width="120" Text="{Binding Source={StaticResource myDataSource}, Path=Name, ValidatesOnDataErrors=True}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

人物.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfPOC
{
public class Person : IDataErrorInfo
{
    public string Name { get; set; }

    #region IDataErrorInfo Members

    public string Error
    {
        get { return string.Empty; }
    }

    public string this[string columnName]
    {
        get { return "Simulated error"; }
    }

    #endregion
}
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您的帮助。

Pet*_*sen 5

发生这种情况是因为用于显示验证错误(在控件周围绘制红色边框)的默认 ControlTemplate 使用 AdornerLayer。

您已经为整个 Window 创建了一个新的 ControlTemplate,并省略了 AdornerDecorator(Window 的默认 ControlTemplate 提供的)

因此,只需使用 AdornerDecorator 包装新的 ControlTemplate,如下所示:

<ControlTemplate TargetType="Window" x:Key="WindowTemplate">
    <AdornerDecorator>
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Control x:Name="FocusCatcher"></Control>
            <TextBlock>Menu Section</TextBlock>
            <ContentPresenter Grid.Row="1" />
            <StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
                <TextBlock Text="Current Editing Mode" />
            </StatusBar>
        </Grid>
    </AdornerDecorator>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)