locked
DataTrigger binding to two controls? RRS feed

  • Question

  • my case should be pretty general. I have a login screen, on which i have an user name textbox, a password box and an OK button.
    I want the OK button to be disabled if any of the user name, password text is empty.
    I am think to use DataTrigger to achieve this. I have written the following code

    <TextBox Height="23" Margin="102,0,12,99" Name="m_edName"
                     VerticalAlignment="Bottom" TabIndex="1" ForceCursor="False" />

    <PasswordBox Height="23" Margin="102,0,12,53" Name="m_edPassword"
                         VerticalAlignment="Bottom" TabIndex="2" />

    <Button Height="23" Margin="65,0,0,12" Name="okBtn"
                    VerticalAlignment="Bottom" Click="okBtn_Click" IsDefault="True"
                    TabIndex="3" HorizontalAlignment="Left" Width="75" Content="OK">
                <Button.Style>
                    <Style TargetType="Button">
                          <Style.Triggers>
                            <DataTrigger
                                  Binding="{Binding ElementName=m_edName, Path=Text}"
                                  Value="">
                                  <Setter Property="Button.IsEnabled" Value="false" />
                            </DataTrigger>
                            <DataTrigger
                                  Binding="{Binding ElementName=m_edPassword, Path=Password}"
                                  Value="">
                                  <Setter Property="Button.IsEnabled" Value="false" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>

    My problem is that it seems only the user name textbox is working. The OK button will not be disabled if the password box is empty.
    Is there a way to specify the relationship between the two DataTriggers? Are they AND relationship or OR relationship?
    Thanks
    James
    Friday, June 26, 2009 8:33 PM

Answers

  • I'm guessing it's because PasswordBox.Password (and PasswordBox.SecurePassword) are not dependency properties.

    I personally use a Command on the button and in the CanExecute handler of the command is the logic to enable/disable the button.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
    Friday, June 26, 2009 9:18 PM
  • Hi,

     

    Both SecurePassword and Password properties of PasswordBox are not dependency property. So they will not benefit the change notification.

    Mark Salsbery ‘s suggestion  using command in this case is a good clue. Alternatively, you can utilize multi-value converter in this scenario.

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    Thursday, July 2, 2009 8:30 AM

All replies

  • I'm guessing it's because PasswordBox.Password (and PasswordBox.SecurePassword) are not dependency properties.

    I personally use a Command on the button and in the CanExecute handler of the command is the logic to enable/disable the button.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
    Friday, June 26, 2009 9:18 PM
  • Hi,

     

    Both SecurePassword and Password properties of PasswordBox are not dependency property. So they will not benefit the change notification.

    Mark Salsbery ‘s suggestion  using command in this case is a good clue. Alternatively, you can utilize multi-value converter in this scenario.

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    Thursday, July 2, 2009 8:30 AM