简体   繁体   English

使按钮IsEnabled状态取决于两个文本框

[英]Making a Button IsEnabled state depend on two textboxes

I want to have a button's IsEnabled to only be true if two textboxes don't have any errors (in this case, I just want them to both be numeric) 我希望有一个按钮的IsEnabled只有两个文本框没有任何错误才会成立(在这种情况下,我只是希望它们都是数字)

I tried setting the textboxes to NotifyOnValidationError=True, thinking this will raise an exception that bubbles up to a container control - I would then set the Button's IsEnabled to true based on the Validation.HasError attached property belonging to that control 我尝试将文本框设置为NotifyOnValidationError = True,认为这会引发一个异常,冒泡到容器控件 - 我会根据属于该控件的Validation.HasError附加属性将Button的IsEnabled设置为true

I tried the following (stripped of formatting code) but it's not working (the button is enabled when I first start the application and the textboxes are empty - they are bound to nullable decimals) 我尝试了以下(删除格式化代码),但它不起作用(当我第一次启动应用程序并且文本框为空时按钮启用 - 它们绑定到可以为空的小数)

<WrapPanel>
    <TextBox x:Name="txtDeltaMin"Text="{Binding Path=DeltaMinFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
    <TextBox x:Name="txtDeltaMax" Text="{Binding Path=DeltaMaxFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
    <Button x:Name="btnUpdateFilter" Click="btnUpdateFilter_Click" IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WrapPanel}}, Path=(Validation.HasError)}">Filter</Button>
</WrapPanel>

You can use a MultiBinding . 您可以使用MultiBinding Should look something like this: 应该看起来像这样:

<TextBox Text="{Binding Path=DeltaMinFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
<TextBox x:Name="txtDeltaMax" Text="{Binding Path=DeltaMaxFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
<Button x:Name="btnUpdateFilter" Click="btnUpdateFilter_Click" Content="Filter">
    <Button.IsEnabled>
        <MultiBinding Converter="{StaticResource IsEnabledConverter}">
            <Binding ElementName="txtDeltaMin" Path="Validation.HasError" />
            <Binding ElementName="txtDeltaMax" Path="Validation.HasError" />
        </MultiBinding>
    </Button.IsEnabled> 
</Button>

Then you just need to make the IsEnabledConverter class by implementing IMultiValueConverter , and add it as a resource. 然后你只需要通过实现IMul​​tiValueConverter来创建IsEnabledConverter类,并将其添加为资源。

public class IsEnabledConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (var isValid in values)
            if (isValid as bool? == false) 
                return false;
        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You could also create a CommandBinding as described in this MSDN article , then use the CanExecuteHandler to Enable/Disable the Button by setting CanExecute . 您也可以按照本MSDN文章中的描述创建CommandBinding,然后使用CanExecuteHandler通过设置CanExecute来启用/禁用Button。

From second link: 从第二个链接:

Typically, a command source, such a MenuItem, will call the CanExecute method on a RoutedCommand to determine if the command can or cannot execute on the current command target. 通常,命令源(如MenuItem)将调用RoutedCommand上的CanExecute方法,以确定该命令是否可以在当前命令目标上执行。 If CanExecute is set to false from an event handler, the command source will disable itself. 如果从事件处理程序将CanExecute设置为false,则命令源将自行禁用。 For example, if a MenuItem is acting as the command source for a command and the command cannot execute on the current command target, then the MenuItem will gray itself out. 例如,如果MenuItem充当命令的命令源,并且命令无法在当前命令目标上执行,则MenuItem将自行灰显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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