Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; if(this.formStackPanel.IsEnabled) {     this.saveButton.Visibility = Visibility.Visible; } else {     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?

like image 853
Jader Dias Avatar asked Mar 06 '12 15:03

Jader Dias


1 Answers

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.

<Window   x:Class="WpfApplication1.Window1"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">   <Window.Resources>     <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>   </Window.Resources>   <StackPanel>     <ToggleButton x:Name="toggleButton" Content="Toggle"/>     <TextBlock       Text="Some text"       Visibility="{Binding IsChecked, ElementName=toggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"/>   </StackPanel> </Window> 
like image 96
Matt Burland Avatar answered Oct 07 '22 07:10

Matt Burland