The class TabItemEx
inherited from TabItem
with an extra int property to record the number of Validation Errors
raised from Controls under it.
internal class TabItemEx : TabItem, INotifyPropertyChanged
{
private int _NumErrors;
public int NumErrors
{
get => _NumErrors;
set
{
if (_NumErrors != value)
{
_NumErrors = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NumErrors)));
}
}
}
public TabItemEx() : base()
{
NumErrors = 0;
Debug.WriteLine("TabItemEx Created");
}
public event PropertyChangedEventHandler PropertyChanged;
}
The Count of Validation Errors was updated each time the Validation.Error
routed event fired.
private void CollectError(object sender, ValidationErrorEventArgs e)
{
var tabItemEx= sender as TabItemEx;
if (tabItemEx == null) return;
if (e.Action == ValidationErrorEventAction.Added)
{
tabItemEx.NumErrors++;
Debug.WriteLine($"{tabItemEx.Header.ToString()}:+:{tabItemEx.NumErrors} {e.Error.ErrorContent.ToString()}");
}
else
{
tabItemEx.NumErrors--;
Debug.WriteLine($"{tabItemEx.Header.ToString()}:-:{tabItemEx.NumErrors} {e.Error.ErrorContent.ToString()}");
}
if (tabItemEx.NumErrors == 0)
{
tabItemEx.Foreground = Brushes.Black;
}
else
{
tabItemEx.Foreground = Brushes.Red;
}
This works fine when load a instance without any Validation Errors in its initial states. (To reproduce, click Normal
button after start up) . The Validation rules are all the three int properties A,B,C should be larger than zero.
What’s strange is the number of errors seems to be wrong. For example, A,B are binding to TabItem SingleProp
.
If I change A from 2 to -2, there should be only 1 Validation Error under SingleProp
but I get 2.
.
What’s stranger is when loading a instance with inproper initial A right after application setup, the # of collected validation errors become zero.
At this state, change A from -2 to 2 would produce a meaningless negtive counts.
Here is a ready-to-run minimal VS project for quick reproduce.
Could any one explain why these happen? And more importantly, how to get the correct error number when loading instance with initial validation errors?