Traitée Datatrigger for OR condition

  • Wednesday, November 28, 2007 11:57 PM
     
     

     

    I have two text boxes, and one button. Where I need to enable the button only when there is some text in both of the text boxes. User cannot leave any of the text box blank.

    I tried using a style with datatrigger, it did not help me.

    The style only works for one text box, meaning if both text boxes have no text then the button is disabled, if any one of the text box has text then the button will be enabled.

    Basically the condition it is looking is AND

    I need an OR condition.

     

    Any help or work arrounds would be appreciated.

     

    my code is like this

     

            <Style x:Key="btnAddStyle" BasedOn="{StaticResource btnStyle}" TargetType="{x:Type Button}">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=txEditName, Path=Text.Length}" Value="0"/>
                            <Condition Binding="{Binding ElementName=txEditDesc, Path=Text.Length}" Value="0"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="False"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>

     

     

    Thanks in advance

All Replies

  • Thursday, November 29, 2007 2:05 AM
     
     Answered
    For a quick solution you could use this:

    Code Block

        <Style x:Key="btnAddStyle" TargetType="{x:Type Button}">
             <Style.Triggers>
                 <MultiDataTrigger>
                     <MultiDataTrigger.Conditions>
                         <Condition Binding="{Binding ElementName=txEditName, Path=Text.Length}" Value="0"/>
                     </MultiDataTrigger.Conditions>
                     <Setter Property="IsEnabled" Value="False"/>
                 </MultiDataTrigger>
                 <MultiDataTrigger>
                     <MultiDataTrigger.Conditions>
                         <Condition Binding="{Binding ElementName=txEditDesc, Path=Text.Length}" Value="0"/>
                     </MultiDataTrigger.Conditions>
                     <Setter Property="IsEnabled" Value="False"/>
                 </MultiDataTrigger>
             </Style.Triggers>
         </Style>


    Using two independent triggers results in the OR functionality you are looking for.

    Hope it suits you,
    Tasos
  • Thursday, November 29, 2007 12:54 PM
     
     

     

    Wow, That was quick.

    Thankyou for your time

     

  • Thursday, November 29, 2007 4:37 PM
     
     

     

    Is there a way to get the OR effect without duplicating code?
  • Thursday, November 29, 2007 6:37 PM
     
     
    I was sure that there was a better solution. Here is one that uses a custom multivalue converter

    Code Block

     class AnyoneOfNumbersEqualToZeroToBooleanConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                //If we have no values return the default bool value
                if (values.Length == 0)
                    return default(bool);
               
                for (int i = 0; i < values.Length; i++)
                {
                    double tempValue;
                    if ( double.TryParse(values[i].ToString(), out tempValue) )
                    {
                        //If any value is zero return false
                        if ( tempValue == 0 )
                            return true;
                    }
                }

                //If we didn't find any value equal to zero then return true
                return false;
            }

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


            <con:AnyoneOfNumbersEqualToZeroToBooleanConverter x:Key="converter"/>
            <Style x:Key="btnAddStyle" TargetType="{x:Type Button}">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Value="True">
                                <Condition.Binding>
                                    <MultiBinding Converter=
                                        "{StaticResource converter}">
                                        <Binding Path="Text.Length" ElementName="txEditDesc"/>
                                        <Binding Path="Text.Length" ElementName="txEditName"/>
                                        <Binding Path="Text.Length" ElementName="txEdit3"/>
                                    </MultiBinding>
                                </Condition.Binding>
                            </Condition>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="False"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>


        <StackPanel>
            <Button Style="{StaticResource btnAddStyle}" Content="Hello"/>
            <TextBox x:Name="txEditName" Text=""/>
            <TextBox x:Name="txEditDesc" Text=""/>
            <TextBox x:Name="txEdit3" Text=""/>
        </StackPanel>


    The converter takes the values provided and searches for double values in them. If any of the values is equal to 0 then it returns true. While the converter could be a lot better it does the job we want. (Sorry about the converter's name,  I really suck at naming converters )

    Then in the window we use this converter inside a multibinding.

    This way you can have multiple values than can be ORed. There surely is a better way but I hope this can get you started.
  • Saturday, July 28, 2012 1:56 AM
     
     

    1. Re. tasosval's solution: Just a little refinement: When there's only one Condition, using a DataTrigger vs. MultiDataTrigger will save some code.

    2. Bbeauchemin's September 18, 2009 12:26 PM reply to the following question contains a general all-purpose workaround for lack of Or option in MultiDataTrigger's: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b10ff310-befe-4c3f-bc8f-d1a134167f77/?prof=required

    3. The easiest way for the O.P.'s specific need without duplicating Setter's would probably be to create and use an IValueConverter that converts non-0 numbers to 1 and then in the XAML, change both Condition's Value's to 1 and then the Setter's Value to True.


    Sent from my iMind