C# 将ControlTemplate应用于窗口时模板不工作时出错

C# 将ControlTemplate应用于窗口时模板不工作时出错,c#,wpf,wpf-controls,controltemplate,errortemplate,C#,Wpf,Wpf Controls,Controltemplate,Errortemplate,我有一个带有App.xaml、MainWindow.xaml和Person类的简单应用程序。当我没有指定模板时,我的ValidateOnDataErrors工作得很好,当文本框出错时会在其周围加上一个红色边框。但是,只要我在MainWindow.xaml的窗口标记中插入“Template=“{StaticResource WindowTemplate}”,该字段仍然有效,但红色边框消失 App.xaml: 提前感谢您的帮助。发生这种情况是因为用于显示验证错误(在控件周围绘制红色边框)的默认控

我有一个带有App.xaml、MainWindow.xaml和Person类的简单应用程序。当我没有指定模板时,我的ValidateOnDataErrors工作得很好,当文本框出错时会在其周围加上一个红色边框。但是,只要我在MainWindow.xaml的窗口标记中插入“Template=“{StaticResource WindowTemplate}”,该字段仍然有效,但红色边框消失

App.xaml:



提前感谢您的帮助。

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

您已经为整个窗口创建了一个新的ControlTemplate,并省略了AdorneDecorator(窗口的默认ControlTemplate提供了该控件)

因此,只需使用AdorneDecorator将新的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>

菜单区

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

您已经为整个窗口创建了一个新的ControlTemplate,并省略了AdorneDecorator(窗口的默认ControlTemplate提供了该控件)

因此,只需使用AdorneDecorator将新的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>

菜单区

这个答案节省了我很多时间,我觉得我应该拥抱某人。非常感谢。这个答案节省了我很多时间,我觉得我应该拥抱某人。非常感谢。
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
}
}
<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>