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!";
}
Con开发者_StackOverflow社区sole.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?
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.
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");
}
}
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}"/>
Try setting the Mode=TwoWay on your binding
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>
You should make the methods implementing the IDataErrorInfo as public.
精彩评论