Hallo Stackoverflow, ребята!
В моей недавней сборке wpf-приложений я обнаружил странное поведение:
Когда я устанавливаю класс Template of the Window в моем приложении, каждый Validation.ErrorTemplate больше не появляется.
Итак, в моем App.xaml я определил следующее:
<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">
<AdornedElementPlaceholder />
</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>
В моем окне я определил следующее:
<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"/>
Класс, к которому я привязал TextBox, выглядит следующим образом:
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; }
}
}
Теперь неожиданно всплывающая подсказка об ошибке появляется, когда имя неверно, но шаблон ошибки остается скрытым. Кто-нибудь может сказать, в чем причина этого или есть ли обходной путь для этого.