开发者

How do I enable (and conversely disable) a button based on the values in multiple other controls?

How do I enable (and conversely disable) a button when a textbox has a valu开发者_运维技巧e, and a combo box has a selected item?

How could I set up the bindings to get the button to disable/enable appropriately?


This is not the way you should think. WPF encourages to use MVVM so you should prepare your VM class so that it has the appropriate properties that you should bind to(and probably model class too). Do not put logic/validation logic into your GUI.


Why not consider to use command binding? See/try for example the following simplified example:

<Window.CommandBindings>
    <CommandBinding Command="Save" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<StackPanel>
    <TextBox Name="TextBox1"/>
    <Button Content="Save" Command="Save"/>
</StackPanel>

CommandBinding has a property [CanExecute] that you can use for enabling/disabling your button in the code behind:

    private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = (this.TextBox1.Text == "test");
    }

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        // put your command-logic here
    }

In this example you have to enter the value "test" to enable the button and execute your command-logic.


Bind the Button to a Command (e.g. Save-Command)
Bind the TextBox.Text to a property (e.g. string MyTextBoxText)
Bind the SelectedItem of the ComboBox to a property (or even the itemSource) (e.g. object MySelectedItem)
The CanExecute of the command has code like this:

return !string.IsNullOrWhiteSpace(MyTextBoxText) && (MySelectedItem != null);


Another way to do this is using a MultiBinding and Converter on the button you want to enable/disable

<Window ... xmlns:local="...">
  <Window.Resources>
    <local:MyMultiValueConverter x:Key="MyMultiValueConverter" />
  </Window.Resources>

  ...

  <ComboBox x:Name="myComboBox">...</ComboBox>
  <TextBox x:Name="myTextBox">...</TextBox>

  ...

  <Button Content="My Button">
    <Button.IsEnabled>
      <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
        <Binding ElementName="myComboBox" Path="SelectedValue" />
        <Binding ElementName="myTextBox" Path="Text" />
      </MultiBinding>
    </Button.IsEnabled>
  </Button>

  ...

</Window>

You need to create an implementation of the IMultiValueConverter interface that tests the values of both the ComboBox.SelectedValue and TextBox.Text properties and return either true or false which will then be assigned to the Button.IsEnabled property. Here's a simple converter that works but you'd want to make sure to tailor one to your specific needs:

public class MyMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values == null)
            return false;

        return values.All(c => c is String ? !String.IsNullOrEmpty((string)c) : c != null);
    }

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

While this approach does work, I tend to agree with the other answers in that you should use commands when possible over multibindings and converters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜