Dzień Dobry,
Mam problem z walidacja danych przy starcie aplikacji w WPF (MVVM) używająć IDataErrorInfo. Walidacja działa OK ale jest jeden mały problem. Przy starcie aplikacji pojawia się błąd co mi lekko nie odpowiada (screen w załączniku). Czy da się zrobić aby walidacja danych działała wyłącznie gdy użytkownik wprowadza dane?
Kod okna:
<Window x:Class="WpfValidation2.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:local="clr-namespace:WpfValidation2"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
Title="MainWindow" Height="161.185" Width="247.149">
<Window.Resources>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,2,0,2"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0"
Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
<Border BorderBrush="Red" BorderThickness="1"/>
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Margin="0,0,40,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="1">First Name:</Label>
<Label Grid.Column="0" Grid.Row="2">Last Name:</Label>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding FirstName,ValidatesOnDataErrors=True}"/>
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding LastName,ValidatesOnDataErrors=True}"/>
</Grid>
</Window>
View Model:
public class MainViewModel : ViewModelBase,IDataErrorInfo
{
public MainViewModel()
{
}
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
RaisePropertyChanged(() => FirstName);
}
}
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName = value;
RaisePropertyChanged(() => LastName);
}
}
public string Error
{
get
{
throw new NotImplementedException();
}
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "FirstName")
{
if (string.IsNullOrEmpty(FirstName))
result = "Please enter a First Name";
}
if (columnName == "LastName")
{
if (string.IsNullOrEmpty(LastName))
result = "Please enter a Last Name";
}
return result;
}
}
}
- okno.JPG (15 KB) - ściągnięć: 131