Я гораздо больше читаю здесь, в stackoverflow, чем писатель, но я отчаянно пытаюсь ответить на этот вопрос. Решения, которые я нашел до сих пор, не устраняют мою проблему: Хотя проверка правильности и отображение правильной ErrorTemplate для TextBox в следующем окне-коде работает (ViewModel за привязкой реализует INotifyDataErrorInfo), тот же самый код для TextBox переместился в пользовательский пользовательский контроль (элементы управления: LabeledTextField), не будет.ErrorTemplate on Control в UserControl
<Window x:Class="ValidationErrorTemplateTest.MainWindow"
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:ignore="http://www.galasoft.ch/ignore"
xmlns:controls="clr-namespace:ValidationErrorTemplateTest"
mc:Ignorable="d ignore"
Height="300"
Width="300"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBox Width="100" Height="20" Text="{Binding WelcomeTitle, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged,
ValidatesOnNotifyDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationErrorHint}" Margin="40"/>
<controls:LabeledTextField Value="{Binding WelcomeTitle}"/>
</StackPanel>
</Grid>
Оболочки/MainSkin.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="validationErrorHint">
<DockPanel LastChildFill="True">
<Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0" Width="12" Height="12" CornerRadius="6"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}">
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" FontSize="10"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</ResourceDictionary>
Это XAML для LabeledTextField:
<UserControl x:Class="ValidationErrorTemplateTest.LabeledTextField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="340">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="Root">
<TextBox Width="100" Height="20" Text="{Binding Value, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged,
ValidatesOnNotifyDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationErrorHint}"/>
</Grid>
и отделенного кода:
namespace ValidationErrorTemplateTest
{
public partial class LabeledTextField : UserControl
{
public LabeledTextField()
{
InitializeComponent();
Root.DataContext = this;
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextField), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}
}
Я прочитал о добавлении <ArdornerDecorator>
-Layer, но, похоже, это не проблема. Я предполагаю, что как-то используется ErrorTemplate usercontrol, а не тот, который назначен для TextBox внутри usercontrol, но я не знаю, как это исправить. Любой намек был бы очень признателен!
большое спасибо! это сработало! Тем не менее, я чувствую разочарование в отношении wpf: я надеялся, что смогу перенести декларативный материал из всех моих текстовых полей в один шаблон usercontrol. Чтобы добавить 'ValidatesOnDataErrors = True}" Validation.ErrorTemplate = "{StaticResource validationErrorHint}", везде вокруг моего проекта довольно уродливое клонирование кода. –