4

我有这个DataGrid

<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,按钮仍处于禁用状态。

4

1 回答 1

4

所以我找到了解决方案:

    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;
        }
    }
于 2013-03-23T14:02:37.130 回答