定义 window 的模板样式时,文本框样式不会由 INotifyDataErrorInfo 触发

Textbox style is not triggered by INotifyDataErrorInfo when having template style for window defined

如果我从 window 中删除样式属性,那么我的文本框周围会出现红色边框。 但是,如果我在 window 上定义了样式,那么它不会显示吗?为什么?

我发现 "problem" 是因为 Template 属性 是在 window 的样式中定义的。当我删除该部分时,验证样式会像应用程序的其他部分一样显示。

这是我 window 的风格:

 <Style TargetType="{x:Type Window}" x:Key="LoginWindowStyle"  >

        <Setter Property="Width" Value="500"/>
        <Setter Property="Height" Value="700"/>
        <Setter Property="ResizeMode" Value="CanResize"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">

                    <Border Background="White" CornerRadius="10">
                        <Border.Effect>
                            <DropShadowEffect ShadowDepth="5" Opacity="0.5"></DropShadowEffect>
                        </Border.Effect>
                        <Grid  Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <StackPanel Height="40" >
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition></ColumnDefinition>

                                        <ColumnDefinition Width="auto" ></ColumnDefinition>

                                    </Grid.ColumnDefinitions>

                                <TextBlock Grid.Column="0" Text="Login" FontSize="15" Margin="30 8 0 0" />
                                    <StackPanel Grid.Column="1" Orientation="Horizontal"  WindowChrome.IsHitTestVisibleInChrome="True">
                                        <Button Style="{DynamicResource WindowTopButtons}" Command="{Binding Minimize}"  Width="40" Height="40">
                                            <TextBlock>-</TextBlock>
                                        </Button>
                                        <Button Style="{DynamicResource WindowTopButtons}" Command="{Binding Maximize}" Margin="1 0 1 0"  Width="40" Height="40">
                                            <TextBlock>[]</TextBlock>
                                        </Button>
                                        <Button Style="{DynamicResource CloseButton}" Command="{Binding Close}"  Width="40" Height="40"  >
                                            <TextBlock>X</TextBlock>
                                        </Button>
                                    </StackPanel>
                                </Grid>

                            </StackPanel>

                            <ContentPresenter  Grid.Row="1"/>


                        </Grid>
                    </Border>


                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

这是我的登录名window:

 <Window x:Class="xxx.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:xxx.ViewModels"
  d:DataContext="{d:DesignInstance local:LoginViewModel,IsDesignTimeCreatable=True}"
    x:Name="LoginWindowName"
    mc:Ignorable="d"     
   WindowState="{Binding CurrentWindowState}"
    Style="{StaticResource LoginWindowStyle}"
    MinWidth="220"
    MinHeight="200"
    WindowStartupLocation="CenterScreen"
    Title="LoginWindow" >
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="bvc" />
</Window.Resources>
<WindowChrome.WindowChrome >
    <WindowChrome ResizeBorderThickness="3" CaptionHeight="40">

    </WindowChrome>
</WindowChrome.WindowChrome>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>
    <StackPanel  Grid.Row="0" Background="{StaticResource LoginGradient}" Margin="0,-5,0,0" >


        <TextBlock Text="Login"
                   VerticalAlignment="Bottom"
                   HorizontalAlignment="Center"
                   Foreground="White" FontSize="50"
                  ></TextBlock>

    </StackPanel>
    <StackPanel Grid.Row="1" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Column="1">
                <StackPanel.Resources>
                    <Style  TargetType="{x:Type TextBox}"  BasedOn="{StaticResource ValidatedTextbox}">
                        <Setter Property="Background" Value="Red"></Setter>
                    </Style>
                </StackPanel.Resources>
                <TextBox  Text="{Binding UserName,Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged,
                    ValidatesOnDataErrors=True,NotifyOnSourceUpdated=True}"
                         Background="LightGray" Height="30" Margin="0 0 0 10"/>

                <TextBox  Style="{StaticResource ValidatedTextbox}"  Text="{Binding Password,Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged,
                    NotifyOnValidationError=True}"
                         Background="LightGray" Height="30"  Margin="0 0 0 10"/>
                <Button Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=LoginWindowName}" Background="LightGray" Height="30">Prijavi se</Button>
                <TextBlock Foreground="Red" Visibility="{Binding IncorrectLogin,Converter={StaticResource bvc}}">Your username and password combination is incorrect</TextBlock>
            </StackPanel>
        </Grid>
    </StackPanel>
</Grid>

在您的 Window 风格中,您需要用 AdornerDecorator 包裹您的 ContentPresenter。验证样式实现为 Adorners,因此如果没有 AdornerDecorator,它们将不会显示。

<AdornerDecorator>
    <ContentPresenter />
</AdornerDecorator>