I'm working on a WPF-application in which I make validation with a mix of IDataErrorInfo and validation rules. For displaying the results of the validation at runtime I have made some styles in the XAML of the window. One of these styles should disable the save-button as long as there is an input error:
<Window.Resources>
<!--Disabling the Save-button by style not viewmodel-property-->
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=tbx_firstname, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=tbx_lastname, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=tbx_age, Path=(Validation.HasError)}" Value="true">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
I have two problems:
The style disables all buttons on the window. What can I do to only disable the save-button? I tried the following, but there seems to be a syntax-error (VS doesn't accept it):
<Style TargetType="{x:Name btn_save}">
In the style for the save-button I have to check each control that is validated. Is there another possibility to make that part shorter and less error-prone (because it has to combined with the viewmodel)?
Thanks in advance!
If you want your style to be applied to a single button instead of all of them, you need to assign a key to the style, e.g.:
<Style x:Key="SaveButtonStyle" TargetType="{x:Type Button}">
That way it will not be applied implicitly to all buttons. To apply it to your save button, explicitly specify the style on the button:
<Button Style="{StaticResource SaveButtonStyle}">