Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

A lot of times, I need to show or hide a textbox based the value of a checkbox. 

unchecked

checked

 

 

In WinForms 2.0.  This was easy:

 

myTextBox.Visible = myCheckBox.Checked;

With the new Visibility Enum in WPF, this becomes a bit trickier.  To accomplish this, you need to implement a converter that will accept a boolean value and return a visibility value.  The converter I used is here:

 

using System; using System.Collections.Generic; using System.Text; using System.Windows.Data; using System.Windows; namespace ParaPlan.Converters { public class BooleanToHiddenVisibility : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Visibility rv = Visibility.Visible; try { var x = bool.Parse(value.ToString()); if (x) { rv = Visibility.Visible; } else { rv = Visibility.Collapsed; } } catch (Exception) { } return rv; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } } }

 

The XAML code to use this is below.  The important parts are:
xmlns:converters="clr-namespace:ParaPara.Converters"
converters:BooleanToHiddenVisibility x:Key="boolToVis"
TextBox ... Visibility={Binding Path=IsChecked, ElementName=checkViewTextBox...}"

 

<Window x:Class="ParaPlan.Windows.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" xmlns:converters="clr-namespace:ParaPlan.Converters"> <StackPanel Orientation="Vertical"> <StackPanel.Resources> <converters:BooleanToHiddenVisibility x:Key="boolToVis"/> </StackPanel.Resources> <CheckBox Content="Check to show text box below me" Name="checkViewTextBox"/> <TextBox Text="only seen when above checkbox is checked" Visibility="{Binding Path=IsChecked, ElementName=checkViewTextBox, Converter={StaticResource boolToVis}}"/> </StackPanel> </Window>

 

Now we can cleanly show or hide an element based on a checkbox using just XAML code.

 

Technorati tags: , ,
Posted on Monday, December 10, 2007 11:24 AM .NET , WPF | Back to top


Comments on this post: WPF - Show/Hide element based on CheckBox.Checked

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
is MSFT crazy?
why do we have Trigger/Setters, but I can't do this in pure XAML?

also, why haven't MSFT provided a bunch of "standard" conversions like this one?

Left by mg on Jan 13, 2008 8:46 PM

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
Hi Tim,

Out of curiosity, have you looked at the BooleanToVisibilityConverter class in System.Windows.Controls? It also handles nullable boolean types.

Keep up the good writing! :)

Brian
Left by Brian Di Croce on Feb 01, 2008 9:37 AM

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
Thanks, it works great for linking MenuItem.IsCheked to Dockings visibility
Left by A.V.Ebrahimi on Apr 19, 2008 8:24 AM

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
And extis BooleanToVisibilityConverter Class. http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx
Left by Pedroafa on Jan 27, 2009 8:04 AM

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
Doesnt work..Get following error

System.Windows.Markup.XamlParseException was unhandled
Message="Cannot convert the value in attribute 'Converter' to object of type 'System.Windows.Data.IValueConverter'. Object of type 'PM.BooleanToHiddenVisibility' cannot be converted to type 'System.Windows.Data.IValueConverter'. Error at object 'System.Windows.Data.Binding' in markup file
Left by Guy on Aug 19, 2009 10:29 PM

# re: WPF - Show/Hide element based on CheckBox.Checked
Requesting Gravatar...
Thank you! That is what I need!
Left by Dimi on Apr 24, 2014 11:30 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free