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
}
}
非常感謝您的幫助。
謝謝,它有效。 – 2012-07-27 15:10:28
這個答案爲我節省了很多時間,我覺得我應該擁抱一個人。謝謝!!!!! – user884248 2017-09-21 14:51:14