0

I have a Combobox With a custom ControlTemplate and I want to change the BackgroundColor based on the Controls Validation.Errors property which is a WPF concept on validation.

I've decided to this by using a Trigger. My ControlTemplate.Triggers is defined like this

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource validationRequiredError}}" Value="True">
       <Setter Property="Background" TargetName="Border" Value="DeepPink"/>
    </DataTrigger>
</ControlTemplate.Triggers>

My Combobox looks like this:

<ComboBox Margin="0" Height="30" HorizontalAlignment="Stretch" VerticalContentAlignment="Center"
                    IsSynchronizedWithCurrentItem="True"
                    DisplayMemberPath="Description"
                    IsEditable="False"
                    Style="{StaticResource ValidateableCombobox}"
                    ItemsSource="{Binding Lists.EstimatedDurations}"
                    SelectedValue="{Binding Model.EstimatedDuration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    SelectedValuePath="Duration"/>

I've sure that the validation defined in FluentValidation is run and added to the Errors Collection, but the converter specified in the Trigger is never run.

I have also checked the Output to verify Binding Errors, but none are shown. Do I need to add something special to my ControlTemplate to make sure the Errors property are bound correctly? Can anyone point me in the correct direction?

Thanks!

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • 1
    Your triggers don't work because 'Validation.Errors' is a read-only attached property. You can't use bindings on a read-only dependency or read-only attached property. The recommended way is to let your view model implement 'INotifyDataErrorInfo' and create a validation error template that meets your design requirements. See my answer on how to do it: https://stackoverflow.com/questions/54023337/how-to-disable-button-on-validation-error/54025419#54025419 – BionicCode Feb 06 '19 at 14:44
  • Thanks @BionicCode I've decided an alternative apporach and setting the Control template in the style.triggers. – Tobias Moe Thorstensen Feb 06 '19 at 14:49

0 Answers0