Validierung - 3. Teil

 

 

Person.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Validierung3Teil
{
public class Person : INotifyPropertyChanged, IDataErrorInfo
{
private string _name;

public string this[string columnName]
{
get
{
string result = string.Empty;

if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
{
if (Name.Length < 3)
{
result = "Der Name braucht mindestens 3 Buchstaben";
}
}
}

return result;
}
}

public string Error
{
get
{
throw new NotImplementedException();
}
}

public string Name
{
get { return _name; }
set
{
_name = value;

if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));

}
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

 


MainWindow.xaml

<Window x:Class="Validierung3Teil.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:Validierung3Teil"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Person x:Key="Person" />
</Window.Resources>
<Grid>
<TextBox Width="150" Height="25" Text="{Binding Source={StaticResource Person}, Path=Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>

 

Top