If I remove style attribute from my window then red border appears around my textboxes. But if I have style defined on window then it is not show? Why?
I figured out that "problem" is because Template
property is defined in styles for window. When I remove that section then validation styling is shown like on other parts of application.
Here is my style for my 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>
This is my login 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>
In your Window
style you need to wrap your ContentPresenter
with an AdornerDecorator
. The validation styles are implemented as Adorner
s, so if there isn't an AdornerDecorator
, they will not be displayed.
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>