我有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,则仍然禁用按钮。
答案 0 :(得分: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;
}
}