Wpf ComboBox验证触发器

时间:2010-10-29 19:29:21

标签: wpf validation

我有一个可编辑的组合框,文本绑定到对象属性。我已将exceptionsValidationrule与text属性相关联,它正在工作并将控件变为红色。我还需要禁用一个按钮,但我无法找到如何在这种情况下检查validation.haserrors

我的XAML用于组合框

<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>

数据触发器

<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>

1 个答案:

答案 0 :(得分:6)

尝试将Path从Validation.HasError更改为(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>

当绑定到附加属性时,应始终包含'('''')'。喜欢

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