wpf 如果两个 TextBox 中的任何一个为空,则禁用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33280097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:29:36  来源:igfitidea点击:

Disable a button if either of the two TextBoxes are empty

c#wpfxaml

提问by Mefhisto1

I have two text boxes and one button. If either of the boxes is empty or white space, the button should be disabled.

我有两个文本框和一个按钮。如果任一框为空或空白,则应禁用该按钮。

<TextBox Name="firstNameTxtBox" />
<TextBox Name="lastNameTxtBox" />

<Button Content="Save" Command="{Binding Save}"  Style="{StaticResource saveButtonEnabler}" />

And the saveButtonEnablerresource:

saveButtonEnabler资源:

<UserControl.Resources>
    <converters:IsButtonEnabledConverter x:Key="isButtonEnabledConverter" />
        <Style x:Key="saveButtonEnabler" TargetType="Button">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=firstNameTxtBox, Path=Text.Length, Converter={StaticResource isButtonEnabledConverter}}" Value="???" />
                        <Condition Binding="{Binding ElementName=lastNameTxtBox, Path=Text.Length, Converter={StaticResource isButtonEnabledConverter}}" Value="???" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
</UserControl.Resources>

Converter gets called appropriately, but I have not idea how to set the result to the Valueof the Condition.

Converter 被适当地调用,但我不知道如何将结果设置ValueCondition.

public class IsButtonEnabledConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value == 0 ? false : true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

回答by Janne Matikainen

Actually you need to use 2 triggers to set IsEnabled to false. Your current implementation is AND and what you want is OR.

实际上,您需要使用 2 个触发器将 IsEnabled 设置为 false。您当前的实现是 AND 而您想要的是 OR。

<TextBox x:Name="firstNameTxtBox"/>
<TextBox x:Name="lastNameTxtBox"/>
<Button Content="Save">
  <Button.Style>
     <Style TargetType="{x:Type Button}">
         <Style.Triggers>
             <DataTrigger Binding="{Binding Text.Length, ElementName=firstNameTxtBox, UpdateSourceTrigger=PropertyChanged}" Value="0">
                 <Setter Property="IsEnabled" Value="False"/>
             </DataTrigger>
             <DataTrigger Binding="{Binding Text.Length, ElementName=lastNameTxtBox, UpdateSourceTrigger=PropertyChanged}" Value="0">
                 <Setter Property="IsEnabled" Value="False"/>
             </DataTrigger>
         </Style.Triggers>
     </Style>
  </Button.Style>
</Button>

If you however want to use the valueconverter approach all you need to do is use 'True' as the Value in your trigger bindings as such

但是,如果您想使用 valueconverter 方法,您需要做的就是在触发器绑定中使用“True”作为值

<Condition Binding="{Binding ElementName=firstNameTxtBox, Path=Text.Length, Converter={StaticResource isButtonEnabledConverter}}" Value="True" />
<Condition Binding="{Binding ElementName=lastNameTxtBox, Path=Text.Length, Converter={StaticResource isButtonEnabledConverter}}" Value="True" />

回答by Salah Akbari

You can use IsEnabledproperty of the button if you have one TextBox:

IsEnabled如果你有一个按钮,你可以使用它的属性TextBox

<Button Content="Save" Command="{Binding Save}" 
        IsEnabled="{Binding ElementName=firstNameTxtBox, Path=Text.Length, Mode=OneWay}" />

For both TextBoxestry this (You don't need to use converter):

对于这两种TextBoxes尝试(您不需要使用转换器):

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding ElementName=firstNameTxtBox, Path=Text.Length, Mode=OneWay}" Value="0"/>
    <Condition Binding="{Binding ElementName=lastNameTxtBox, Path=Text.Length, Mode=OneWay}" Value="0"/>
</MultiDataTrigger.Conditions>

Your complete code would be:

您的完整代码将是:

<TextBox Name="firstNameTxtBox" />
<TextBox Name="lastNameTxtBox" />

<Button Content="Save" Command="{Binding Save}" >
      <Button.Style>
          <Style TargetType="{x:Type Button}">
              <Style.Triggers>
                  <MultiDataTrigger>
                      <MultiDataTrigger.Conditions>
                         <Condition Binding="{Binding ElementName=firstNameTxtBox, Path=Text.Length, Mode=OneWay}" Value="0"/>
                         <Condition Binding="{Binding ElementName=lastNameTxtBox, Path=Text.Length, Mode=OneWay}" Value="0"/>
                      </MultiDataTrigger.Conditions>
                      <Setter Property="IsEnabled" Value="False"/>
                 </MultiDataTrigger>
             </Style.Triggers>
          </Style>
      </Button.Style>
</Button>

回答by Dennis

This will work without converter:

这将在没有转换器的情况下工作:

    <TextBox x:Name="Foo1"/>
    <TextBox x:Name="Foo2"/>
    <Button Content="Push me">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Text.Length, ElementName=Foo1, UpdateSourceTrigger=PropertyChanged}" Value="0"/>
                            <Condition Binding="{Binding Text.Length, ElementName=Foo2, UpdateSourceTrigger=PropertyChanged}" Value="0"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="False"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

回答by Nawed Nabi Zada

Your converter is returning a boolean, so you definitely have to set your value to True.

您的转换器返回一个布尔值,因此您肯定必须将值设置为 True。

But I would change my converter a little bit: Instead of:

但我会稍微改变我的转换器:而不是:

return (int)value == 0 ? false:true;

I would do:

我会做:

return ((int)value) !=0;