开发者

Window.Template kills any Validation.ErrorTemplate?

Hallo Stackoverflow fellas!

In my recent wpf application building I ancountered a strange behaviour:

When I set the Template of the Window class in my application every Validation.ErrorTemplate doesn't appear anymore.

So in my App.xaml I have defined the following:

<Application.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="WhiteSmoke"/>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="12">*</TextBlock>
                        <Border BorderBrush="Red" BorderThickness="2" CornerRadius="3">
                            <AdornedEl开发者_Python百科ementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="PlainStyle" TargetType="{x:Type l:MainWindow}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type l:MainWindow}">
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>        
</Application.Resources>

In my Window I defined the following:

<Window x:Class="ModelItemTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    Height="600" 
    Width="800" 
    Style="{StaticResource PlainStyle}">
<TextBox x:Name="Testbox" Text="{Binding Path=TestPerson.Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100"/>

The class I bound the TextBox to looks like this:

public class Person : IDataErrorInfo
{
    private string _name;

    public string Name { get { return _name; } set { _name = value; } }

    public string this[string columnName]
    {
        get
        {
            if (columnName.Equals("Name") && !Name.Equals("Martin"))
                return "The Value is invalid!!!";
            return string.Empty;
        }
    }

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

Now surprisingly the tooltip of the error shows up when the name is invalid but the error template stays hidden. Can anybody tell what is the reason for that or if there is a work around for that.


You have removed the AdornerDecorator from the default Window template, so there is no AdorderLayer to render the error adorner. Try adding one to your ControlTemplate:

<ControlTemplate TargetType="{x:Type l:MainWindow}">
    <AdornerDecorator>
        <ContentPresenter Content="{TemplateBinding Content}"/>
    </AdornerDecorator>
</ControlTemplate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜