从 wpf 客户端验证 ViewModel 中的 Entity Framework 对象
Validate Entity Framework object in ViewModel from wpf client side
在我的 WPF MVVM-Light ViewModel class 中,我有一个 属性,这是一个来自 EntityFramework 6
的 EntityObject
public Client Client
{
get { return client; }
set
{
client = value;
this.RaisePropertyChanged(() => this.Client);
}
}
部分客户端class:
[MetadataType(typeof(ClientMetadata))]
[CustomValidation(typeof(ClientValidator), "ClientValidation")]
public partial class Client
{
public sealed class ClientMetadata
{
[Display(ResourceType = typeof(CaptionResources), Name = "Name")]
public string Name { get; set; }
}
}
它绑定到视图中的许多控件,例如:
<TextBox TextWrapping="Wrap" Margin="5" Height="36" Text="{Binding Client.Name, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, NotifyOnSourceUpdated=True}" TabIndex="1"
Validation.ErrorTemplate="{StaticResource ImagedErrorTemplate}"/>
如何在视图上显示验证结果?我已经在我的 ViewModel 中实现了 INotifyDataErrorInfo, IValidationErrors
个接口。我已经有一个验证对象和 returns 验证错误的方法:
private bool Validate()
{
var errors = new Dictionary<string, string>();
if (!IsValid<Client, Client.ClientMetadata>(this.Client, ref errors))
{
foreach (var error in errors)
{
this.RaiseErrorsChanged("Client." + error.Key);
this.RaisePropertyChanged(string.Format("Client.{0}", error.Key));
}
return false;
}
return true;
}
但我仍然无法在视图中获取此信息。我的错误模板适用于 "standard" 属性:
<ControlTemplate x:Key="ImagedErrorTemplate">
<DockPanel >
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder x:Name="adorner"/>
</Border>
<Image Source="../Assets/Images/warning.png"
Height="20"
Width="20"
ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
</DockPanel>
</ControlTemplate>
没有 DTO 对象,有什么方法可以做到这一点吗?
提前致谢。
我已经通过在 EntityFramework 个对象上实现 INotifyDataErrorInfo 来做到这一点。
[MetadataType(typeof(ClientMetadata))]
[CustomValidation(typeof(ClientValidator), "ClientValidation")]
public partial class Client : INotifyDataErrorInfo
{
private Dictionary<string, ICollection<string>> validationErrors = new Dictionary<string, ICollection<string>>();
public sealed class ClientMetadata
{
[Display(ResourceType = typeof(CaptionResources), Name = "Name")]
public string Name { get; set; }
}
public Dictionary<string, ICollection<string>> ValidationErrors
{
get
{
return this.validationErrors;
}
set
{
this.validationErrors = value;
}
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) || !this.ValidationErrors.ContainsKey(propertyName))
{
return null;
}
return this.ValidationErrors[propertyName];
}
public bool HasErrors
{
get { return this.ValidationErrors.Count > 0; }
}
public void RaiseErrorsChanged(string propertyName)
{
if (this.ErrorsChanged != null)
{
this.ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
}
在我的 WPF MVVM-Light ViewModel class 中,我有一个 属性,这是一个来自 EntityFramework 6
的 EntityObject public Client Client
{
get { return client; }
set
{
client = value;
this.RaisePropertyChanged(() => this.Client);
}
}
部分客户端class:
[MetadataType(typeof(ClientMetadata))]
[CustomValidation(typeof(ClientValidator), "ClientValidation")]
public partial class Client
{
public sealed class ClientMetadata
{
[Display(ResourceType = typeof(CaptionResources), Name = "Name")]
public string Name { get; set; }
}
}
它绑定到视图中的许多控件,例如:
<TextBox TextWrapping="Wrap" Margin="5" Height="36" Text="{Binding Client.Name, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, NotifyOnSourceUpdated=True}" TabIndex="1"
Validation.ErrorTemplate="{StaticResource ImagedErrorTemplate}"/>
如何在视图上显示验证结果?我已经在我的 ViewModel 中实现了 INotifyDataErrorInfo, IValidationErrors
个接口。我已经有一个验证对象和 returns 验证错误的方法:
private bool Validate()
{
var errors = new Dictionary<string, string>();
if (!IsValid<Client, Client.ClientMetadata>(this.Client, ref errors))
{
foreach (var error in errors)
{
this.RaiseErrorsChanged("Client." + error.Key);
this.RaisePropertyChanged(string.Format("Client.{0}", error.Key));
}
return false;
}
return true;
}
但我仍然无法在视图中获取此信息。我的错误模板适用于 "standard" 属性:
<ControlTemplate x:Key="ImagedErrorTemplate">
<DockPanel >
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder x:Name="adorner"/>
</Border>
<Image Source="../Assets/Images/warning.png"
Height="20"
Width="20"
ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
</DockPanel>
</ControlTemplate>
没有 DTO 对象,有什么方法可以做到这一点吗?
提前致谢。
我已经通过在 EntityFramework 个对象上实现 INotifyDataErrorInfo 来做到这一点。
[MetadataType(typeof(ClientMetadata))]
[CustomValidation(typeof(ClientValidator), "ClientValidation")]
public partial class Client : INotifyDataErrorInfo
{
private Dictionary<string, ICollection<string>> validationErrors = new Dictionary<string, ICollection<string>>();
public sealed class ClientMetadata
{
[Display(ResourceType = typeof(CaptionResources), Name = "Name")]
public string Name { get; set; }
}
public Dictionary<string, ICollection<string>> ValidationErrors
{
get
{
return this.validationErrors;
}
set
{
this.validationErrors = value;
}
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) || !this.ValidationErrors.ContainsKey(propertyName))
{
return null;
}
return this.ValidationErrors[propertyName];
}
public bool HasErrors
{
get { return this.ValidationErrors.Count > 0; }
}
public void RaiseErrorsChanged(string propertyName)
{
if (this.ErrorsChanged != null)
{
this.ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
}