2012-02-01 58 views
0

我已經在WPF(MVVM)中構建了一個應用程序並添加了驗證。這裏是我的結果:在WPF中綁定數據時的驗證

enter image description here

你會發現周圍的輸入文本框的紅色鄉紳。事情是,我不想在開始表單時與驗證錯誤對抗客戶端。輸入數據或按提交將是最好的。

在SO上有一些類似的問題,但還沒有找到合適的解決方案。 (重新初始化上的每個控件是無解)

所以,問題是如何啓動的形式:

選項A:(容易)重置確認

選項B:不要打電話確認直到結合

選項C後:其它很好的解決方案

下載代碼:

here

查看代碼:

<Window x:Class="Validation.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="150" Width="250"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"></RowDefinition> 
      <RowDefinition Height="auto"></RowDefinition> 
      <RowDefinition Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="80"></ColumnDefinition> 
      <ColumnDefinition></ColumnDefinition> 
     </Grid.ColumnDefinitions> 
     <!-- Name --> 
     <Label Grid.Column="0" Grid.Row="0">Name</Label> 
     <TextBox Grid.Column="1" Grid.Row="0" 
       Text="{Binding Model.Name, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"></TextBox> 
     <!-- Age --> 
     <Label Grid.Column="0" Grid.Row="1">Age</Label> 
     <TextBox Grid.Column="1" Grid.Row="1" 
       Text="{Binding Model.Age, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"></TextBox> 
     <!-- Submit/Cancel --> 
     <StackPanel Grid.Column="1" Grid.Row="2" FlowDirection="LeftToRight"> 
      <Button>Cancel</Button> 
      <Button>Submit</Button> 
     </StackPanel> 
    </Grid> 
</Window> 

視圖模型的代碼:

public class FormViewModel 
{ 
    #region constructors 
    public FormViewModel() 
    { 
     Model = new FormModel(); 
    } 
    #endregion 

    #region properties 
    public FormModel Model { get; set; } 
    #endregion 

} 

型號代碼:

public class FormModel : INotifyPropertyChanged, IDataErrorInfo 
    { 
     #region notifypropertychanged 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = this.PropertyChanged; 
      if (handler != null) 
      { 
       var e = new PropertyChangedEventArgs(propertyName); 
       handler(this, e); 
      } 
     } 
     #endregion 

     #region dataerrorinfo 
     public string Error 
     { 
      get { return null; } 
     } 

     public string this[string columnName] 
     { 
      get 
      { 
       switch (columnName) 
       { 
        case "Name": 
         if (string.IsNullOrEmpty(Name)) 
         { 
          return "Name is required"; 
         } 
         break; 
        case "Age": 
         if (Age < 18 || Age > 50) 
         { 
          return "Are you kidding?"; 
         } 
         break; 
       } 
       return null; 
      } 
     } 
     #endregion 

     #region properties 
     private string name; 
     public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } } 

     private int age; 
     public int Age { get { return age; } set { age = value; OnPropertyChanged("Age"); } } 
     #endregion 
    } 

解決方案(目前)

enter image description here

<Style x:Key="InputControlDefault" TargetType="{x:Type TextBox}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <DockPanel LastChildFill="True"> 
        <TextBlock DockPanel.Dock="Right" 
        Foreground="black" 
        FontSize="12pt"> 
        ? 
        </TextBlock> 
        <AdornedElementPlaceholder/> 
       </DockPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsEnabled" Value="false"> 
      <Setter Property="Foreground" Value="Black"/> 
     </Trigger> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" Value="test"/> 
      <Setter Property="BorderBrush" Value="Red" /> 
     </Trigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="Validation.HasError" Value="true"/> 
       <Condition Property="Text" Value=""/> 
      </MultiTrigger.Conditions> 
      <Setter Property="BorderBrush" Value="Orange" /> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <DockPanel LastChildFill="True"> 
          <TextBlock DockPanel.Dock="Right" Foreground="Black" FontSize="12pt"> 
           * 
          </TextBlock> 
          <AdornedElementPlaceholder/> 
         </DockPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="ToolTip" Value="test"/> 
     </MultiTrigger> 
    </Style.Triggers> 
</Style> 

回答

1

我回答過類似的問題,前幾天:

首先,如果您的規則說,第一次和姓氏不應該是空的 - 它的權利,用戶看到驗證錯誤。

我所做的是對空/初始值使用ValidationTemplate,所以 表示用戶只需看到一個「*」表示請求字段。

here是完整的答案

+0

我喜歡你的方法,會嘗試一下。 – 2012-02-01 12:45:34

+0

試過,並將結果添加到問題中。 – 2012-02-01 13:22:44