Witam, nie mam już zielonego pojęcia czemu mi to nie działa. Generalnie obramowanie TextBox-a zapala się na czerwono ale żaden komunikat pod spodem się nie pojawia, jeśli ktoś może spojrzeć na kod, byłbym wdzięczny za pomoc.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2.Models"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Model x:Key="Model" />
</Window.Resources>
<Grid>
<StackPanel DataContext="{StaticResource Model}" Margin="5" Width="100" HorizontalAlignment="Left">
<TextBox x:Name="CostumerNames" Text="{Binding CostumerName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="{Binding ElementName=CostumerNames, Path=(Validation.Errors)CurrentItem.ErrorContent}" />
</StackPanel>
</Grid>
</Window>
public class Model:INotifyPropertyChanged, IDataErrorInfo
{
private string costumerName;
public string CostumerName
{
get { return costumerName; }
set
{
costumerName = value;
OnPropertyChanged("CostumerName");
}
}
#region IDataErrorInfo
public string Error
{
get { return null; }
}
public string this[string propertyName]
{
get
{
return GetValidationError(propertyName);
}
}
public bool IsValid
{
get
{
foreach (string property in ValidatedProperty)
if (GetValidationError(property) != null)
return false;
return true;
}
}
public string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "CostumerName":
error = ValidateCustomerName();
break;
}
return error;
}
public string ValidateCustomerName()
{
if (String.IsNullOrEmpty(CostumerName))
return "Uzupełnij swoje imie";
return null;
}
static readonly string[] ValidatedProperty =
{
"CustomerName"
};
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}