简体   繁体   中英

Wpf ComboBox validation Trigger

I have a editable combobox that text is bound to an object property. I have associated a exceptionsValidationrule with the text property and it is working and turning the control red. I need to also disable a button but I can't seam to find how to check the validation.haserrors in this case

my XAML for the combo box

<ComboBox Margin="0,3,0,3"  Width="40" Name="CATCODE" IsEditable="True" >
    <ComboBox.Text>
       <Binding Path="CategoryCode" >
           <Binding.ValidationRules>
                <ExceptionValidationRule >

                </ExceptionValidationRule>
           </Binding.ValidationRules>
       </Binding>
    </ComboBox.Text>
</ComboBox>

The data trigger

<Style x:Key="DisbleOnValidation"  TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Validation.HasError, ElementName=CATCODE}" Value="True" >
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Try to change Path from Validation.HasError to (Validation.HasError)

<Style x:Key="DisbleOnValidation"  TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=(Validation.HasError), ElementName=CATCODE}" Value="True" >
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

When Binding to Attached Properties, one should always include the '('...')'. Like

{Binding Path=(Grid.Row), ElementName=SomeElement}
{Binding Path=(Canvas.Left), ElementName=SomeOtherElement}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM