C# – How to bind a control’s property to another control’s property


I want that the SaveButton from my form to dissapear when the form is disabled.
I do that this way:

this.formStackPanel.IsEnabled = someValue;
    this.saveButton.Visibility = Visibility.Visible;
    this.saveButton.Visibility = Visibility.Collapsed;

Isn't there a way of binding those properties in the XAML? Is there a better way of doing that?

Best Solution

Yes. You should be able to bind the stackpanel's IsEnabled to your button's Visibility property. However, you need a converter. WPF comes with a BooleanToVisibilityConverter class that should do the job.

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <ToggleButton x:Name="toggleButton" Content="Toggle"/>
      Text="Some text"
      Visibility="{Binding IsChecked, ElementName=toggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"/>