2016-04-20 95 views
0

以我WPF MVVM光強ViewModel類我有一個屬性,也就是從的EntityFramework 6驗證實體框架對象從WPF客戶端側

public Client Client 
    { 
     get { return client; } 
     set 
     { 
      client = value; 
      this.RaisePropertyChanged(() => this.Client); 
     } 
    } 
Client類的

部分一部分的EntityObject:

[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}"/> 

我怎麼能disp已在視圖上放置驗證結果?我已經在我的ViewModel中實現了INotifyDataErrorInfo, IValidationErrors接口。我已經有一個方法驗證對象並返回驗證錯誤:

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; 
    } 

但我仍然無法在視圖中獲取此信息。我的錯誤模板適用於「標準」屬性:

<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對象?

在此先感謝。

+0

您是否需要在控制模板中爲您的錯誤顯示錯誤消息的文本塊? – XAMlMAX

+0

它沒有區別。我想對大多數控件進行驗證,而不僅僅是文本塊。 – Bruniasty

+0

我不是那個意思,我的意思是當你使用文本塊發生錯誤時,你的控件模板需要顯示一些文本。不只驗證文本塊。希望這是有道理的 – XAMlMAX

回答

0

我已經設法通過在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)); 
     } 
    }