目前我面临一个我无法解决的荒谬问题
我写了一个小包装器,它包装了几乎所有属性并添加了一个属性,但我不知道如何通过他将验证传递给我的 XAML
这是我的代码
XAML
<TextBox Height="23" HorizontalAlignment="Left" Margin="42,74,0,0" Name="textBox2" VerticalAlignment="Top" Width="120"
DataContext="{Binding TB2}"/>
<!-- this Style is be added to the parent of TextBox -->
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding Value,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsDirty}" Value="true">
<Setter Property="BorderBrush" Value="Orange"/>
</DataTrigger>
</Style.Triggers>
</Style>
视图模型
public class vm : IDataErrorInfo, INotifyPropertyChanged
{
[Required]
[Range(4, 6)]
public string TB1 { get; set; }
[Required]
[Range(4, 6)]
public myWrapper TB2
{
get { return tb2; }
set{
tb2 = value;
OnPropertyChanged("TB2");
}
}
private myWrapper tb2;
public vm()
{
TB1 = "";
tb2 = new myWrapper("T");
}
#region IDataErrorInfo
private Dictionary<string, string> ErrorList = new Dictionary<string, string>();
public string Error { get { return getErrors(); } }
public string this[string propertyName] { get { return OnValidate(propertyName); } }
private string getErrors()
{
string Error = "";
foreach (KeyValuePair<string, string> error in ErrorList)
{
Error += error.Value;
Error += Environment.NewLine;
}
return Error;
}
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Invalid property name", propertyName);
string error = string.Empty;
var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
var results = new List<ValidationResult>(2);
var context = new ValidationContext(this, null, null) { MemberName = propertyName };
var result = Validator.TryValidateProperty(value, context, results);
if (!result)
{
var validationResult = results.First();
error = validationResult.ErrorMessage;
}
if (error.Length > 0)
{
if (!ErrorList.ContainsKey(propertyName))
ErrorList.Add(propertyName, error);
}
else
if (ErrorList.ContainsKey(propertyName))
ErrorList.Remove(propertyName);
return error;
}
#endregion //IDataErrorInfo
#region INotifyPropertyChanged
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
我的包装器
public class myWrapper : INotifyPropertyChanged
{
private object currentValue;
private object currentOriginal;
public object Value
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("Value");
OnPropertyChanged("IsDirty");
}
}
public bool IsDirty
{
get { return !currentValue.Equals(currentOriginal); }
}
#region cTor
public myWrapper(object original)
{
currentValue = original;
currentOriginal = original.Copy(); // creates an deep Clone
}
#endregion
#region INotifyPropertyChanged
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
我还在 myWrapper 中测试了 IDataErrorInfo 没有运气