Reputation: 3077
A note - the classes I have are EntityObject
classes!
I have the following class:
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar : IDataErrorInfo
{
public string Name { get; set; }
#region IDataErrorInfo Members
string IDataErrorInfo.Error
{
get { return null; }
}
string IDataErrorInfo.this[string columnName]
{
get
{
if (columnName == "Name")
{
return "Hello error!";
}
Console.WriteLine("Validate: " + columnName);
return null;
}
}
#endregion
}
XAML goes as follows:
<StackPanel Orientation="Horizontal" DataContext="{Binding Foo.Bar}">
<TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true}"/>
</StackPanel>
I put a breakpoint and a Console.Writeline
on the validation there - I get no breaks. The validation is not executed. Can anybody just press me against the place where my error lies?
Upvotes: 4
Views: 7083
Reputation: 23
You should create local window resource containing Bar class reference and use its key to set StackPanel data context property. Also, don't forget to import its namespace in the window or user control.
Your XAML code should be like following:
<Window x:Class="Project.WindowName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BarNamespace">
<Window.Resources>
<local:Bar x:Key="bar" />
</Window.Resources>
<StackPanel Orientation="Horizontal" DataContext="{StaticResource bar}">
<TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true}"/>
</StackPanel>
</Window>
Upvotes: 1
Reputation: 12321
This may be a silly answer, but by default the binding calls the setter when LostFocus
happens. Try doing that if you haven't done that.
If you want the error code to be triggered on every key press, Use UpdateSourceTrigger=PropertyChanged
inside the binding.
Upvotes: 2
Reputation: 39
You should make the methods implementing the IDataErrorInfo as public.
Upvotes: -1
Reputation: 5724
I am not familiar with the EntityObject class, nor can I find it in the .NET Framework documentation or a quick google search.
Anyway, what you need to do us use NotifyOnValidationError as well:
<TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
Upvotes: 1
Reputation: 50028
You forgot to implement INotifyPropertyChanged on the 'Bar' class, then only the binding system will trigger the setter.
So your 'Name' property should most likely be.
public string Name
{
get{ return _name; }
set
{
_name = value;
RaisePropertyChanged("Name"); // Or the call might OnPropertyChanged("Name");
}
}
Upvotes: 1