Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a button if either of the two TextBoxes are empty

Tags:

c#

wpf

xaml

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 saveButtonEnabler resource:

<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 Value of the Condition.

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();
    }
}
like image 694
Mefhisto1 Avatar asked Oct 22 '15 11:10

Mefhisto1


Video Answer


1 Answers

Actually you need to use 2 triggers to set IsEnabled to false. Your current implementation is AND and what you want is 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

<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" />
like image 198
Janne Matikainen Avatar answered Sep 17 '22 15:09

Janne Matikainen