Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this is an easy question, but I have been out of the programming world for 3 years so please help!

If I have a checkbox on a WPF Page how do I hide a textbox if the checkbox is not ticked?

Thanks in advance
Posted
Updated 25-May-10 22:43pm
v2

Hey,

You can bind the Textbox's Visibility property to the CheckBox's Checked property and use a BooleanToVisibilityConverter to make the types match. That should do it.
Let me know if you need more clarity.
 
Share this answer
 
Comments
Suziem 26-May-10 4:49am    
Yes please how do I do this?
Ok here is a sample window with what I think you are asking for. When you click the checkbox, the textblock becomes visible.


<Window x:Class="WpfFrontEnd.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300">
<Window.Resources>

<BooleanToVisibilityConverter x:Key="boolToVisible"/>

</Window.Resources>
<Grid>

<StackPanel>
<CheckBox x:Name="CheckBox1"/>
<TextBlock Visibility="{Binding ElementName=CheckBox1, Path=IsChecked, Converter={StaticResource boolToVisible}}">Hello</TextBlock>
</StackPanel>

</Grid>
</Window>
 
Share this answer
 
v2
Comments
theminidriver 26-May-10 5:11am    
Please note the revised version, for some reason the code got mangled on the first cut and paste.
Suziem 26-May-10 5:18am    
Thanks a million!!
Suziem 26-May-10 7:12am    
Me again: if you want to do this for a checkbox which is unchecked what command would you use?
theminidriver 26-May-10 10:41am    
Do you mean show the textbox when the checkbox is Unchecked and vice-versa?
In that case , you will probably need to implement your own value converter, instead of using the provided BooleanToVisibilityConverter. Look up IValueConverter on MSDN documentation, If I remember correctly there is an example.

In any event it is quite easy to figure it out- just make a class which implements IValueConverter and fill in the method stubs. Then use your class instead of BooleanToVisibilityConverter as the Converter in the binding.

Hope that helps!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900