2012-07-27 37 views
0

我有一個App.xaml,MainWindow.xaml和Person類的簡單應用程序。當我沒有指定模板時,我的ValidateOnDataErrors完美工作,當它出錯時,在我的文本框周圍放置一個紅色邊框。但是,只要在我的MainWindow.xaml的Window標記中插入'Template =「{StaticResource WindowTemplate}」',該字段仍然有效,但紅色邊框消失。將ControlTemplate應用於窗口時,錯誤模板不起作用

App.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> 

MainWindow.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> 

Person.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 
} 
} 

非常感謝您的幫助。

回答

4

發生這種情況是因爲用於顯示驗證錯誤(在控件周圍繪製紅色邊框)的默認ControlTemplate使用AdornerLayer。

您已經創建了整個窗口的新的ControlTemplate,並留出了AdornerDecorator(這對於窗口用品的默認控件模板)

所以只要有AdornerDecorator包裝你的新控件模板,像這樣:

<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> 
+0

謝謝,它有效。 – 2012-07-27 15:10:28

+0

這個答案爲我節省了很多時間,我覺得我應該擁抱一個人。謝謝!!!!! – user884248 2017-09-21 14:51:14