2013-03-22 73 views
4

按鈕我有這個DataGrid禁用/啓用與DataGridTextColumn驗證

<DataGrid ItemsSource="{Binding CustomerXml}" 
    Name="customersDataGrid" AutoGenerateColumns="False" DataContext="{Binding}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Name"> 
      <DataGridTextColumn.Binding> 
       <Binding Path="Name" ValidatesOnDataErrors="True" 
        UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True"> 
        <Binding.ValidationRules> 
         <local:NameValidationRule/> 
        </Binding.ValidationRules> 
       </Binding> 
      </DataGridTextColumn.Binding> 
     </DataGridTextColumn> 

     ... 

    </DataGrid.Columns>   
</DataGrid> 

我試圖禁用和ValidationRule結果使保存按鈕。

<Button Content="Save" Name="save" Click="save_Click"> 
    <Button.Style> 
     <Style TargetType="{x:Type Button}"> 
      <Setter Property="IsEnabled" Value="false" /> 
      <Style.Triggers> 
       <MultiDataTrigger> 
        <MultiDataTrigger.Conditions> 
         <Condition 
          Binding="{Binding ElementName=customersDataGrid, 
          Path=(Validation.HasError)}" Value="false" /> 
        </MultiDataTrigger.Conditions> 
        <Setter Property="IsEnabled" Value="true" /> 
       </MultiDataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
</Button> 

但如果ValidationResult設置爲true或false,按鈕仍然被禁用。

回答

3

於是我找到解決辦法:

int count = 0; 

    private void customersDataGrid_Error(object sender, ValidationErrorEventArgs e) 
    { 
     if (e.Action == ValidationErrorEventAction.Added) 
     { 
      save.IsEnabled = false; 
      count++; 
     } 
     if (e.Action == ValidationErrorEventAction.Removed) 
     { 
      count--; 
      if (count == 0) save.IsEnabled = true; 
     } 
    }