Reputation: 1163
I have created a style in order to specify the validation ControlTemplate to use when there is a validation error in some of my textboxes. The validation errors get caught, as I can see my style being used (the default red border for the textbox, and my added light pink background with a red "!!!" string), but the problem is the "!!!" red string is always there, even though there are no validation errors (though the pink background and red border disappear). I am using IDataErrorInfo in order to validation the textboxes.
Here is my xaml style code:
<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="LightPink"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<AdornedElementPlaceholder x:Name="ControlWithError"/>
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="12pt"
FontWeight="Bold"
Margin="-18,0,0,0"
Text="!!!">
</TextBlock>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I tried adding this line to the < TextBlock > node but it didn't work:
Visibility="{Binding Validation.HasError, Source={RelativeSource Self}, Converter={StaticResource BoolToHiddenOrVisibleConverter}}"
My question is: How can I make the "!!!" red string only appear when the Validation.HasError flag is set to true?
Here is how I declared one of my textboxes, as a reference. The OneTextBox is a control that encapsulates the regular WPF TextBox and adds some functionalities (as I am using a custom framework):
<pres:OneTextBox Grid.Row="0" Watermark="Name..." Margin="85,12,0,0" Style="{StaticResource ErrorValidationTextBox}"
Text="{Binding Path=InterfaceSpecification.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
AcceptsReturn="False" MaxLines="1" Height="22" VerticalAlignment="Top"
HorizontalAlignment="Left" Width="300" />
EDIT: I am getting the following binding error when running in debug:
BindingExpression path error: 'Validation' property not found on 'object' ''RelativeSource' (HashCode=58276509)'. BindingExpression:Path=Validation.HasError; DataItem='RelativeSource' (HashCode=58276509); target element is 'TextBlock' (Name=''); target property is 'Visibility' (type 'Visibility')
EDIT2: Here is how I implement the IDataErrorInfo in my class:
public string Error
{
get { return mError; }
set { mError = value; }
}
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Name":
if (string.IsNullOrWhiteSpace(Name))
{
Error = "The name cannot be null, empty or contain only white spaces";
}
else if (Name.StartsWith(" "))
{
Error = "The name cannot start with a white spaces";
}
else if (Name.IndexOfAny(Path.GetInvalidPathChars()) != -1)
{
Error = "The name cannot contain invalid characters";
}
else
{
Error = null;
}
break;
}
return Error;
}
}
Upvotes: 0
Views: 759
Reputation: 3683
This is surely working
<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="LightPink"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<AdornedElementPlaceholder x:Name="ControlWithError"/>
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="12pt"
FontWeight="Bold"
Margin="-18,0,0,0"
Text="!!!"
Visibility="{Binding ElementName=ControlWithError, Path=AdornedElement.(Validation.HasError),Converter={StaticResource BoolToHiddenOrVisibleConverter}}" >
</TextBlock>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
Upvotes: 1