Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBox Show/Hide TextBox WPF

Tags:

c#

.net

mvvm

wpf

xaml

As the title says, Iam trying to show/hide a TextBox in WPF without writing code in MainWindow.xaml.cs file.

Model:

public class Person
{
    public string Comment { get; set; }
}

View:

<Window x:Class="PiedPiper.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="WPF" Height="400" Width="400">

    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden" Name="CommentTextBox"></TextBox>
</Grid>

ViewModel:

    public class PersonViewModel : INotifyPropertyChanged
    {
    public PersonViewModel(Person person)
    {
        Comment = person.Comment;
    }

    private string _comment;
    public string Comment
    {
        get { return _comment; }
        set { _comment = value; OnPropertyChanged("Comment"); }
    }

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

So the TextBox should be hidden at start, but visible when checkbox is checked. Please help!

Thanks.

like image 495
Andre Korosh Kordasti Avatar asked Oct 29 '14 10:10

Andre Korosh Kordasti


2 Answers

The simplest way is to write a custom "BooleanToHiddenVisibilityConverter" and use it (like dkozl said). It's a really simple converter and it comes in handy in many situations. I think that every descent WPF application should have one.

public sealed class BooleanToHiddenVisibilityConverter : IValueConverter
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        bool bValue = false;
        if (value is bool) 
        {
            bValue = (bool)value;
        }
        return (bValue) ? Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility) 
        {
            return (Visibility)value == Visibility.Visible; 
        }
        return false;
    }
}

And use it like dkozl said:

<StackPanel>
    <StackPanel.Resources>
        <BooleanToHiddenVisibilityConverter x:Key="BooleanToHiddenVisibilityConverter"/>
    </StackPanel.Resources>
    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox 
        Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" 
        Visibility="{Binding ElementName=CommentCheckBox, Path=IsChecked, 
        Converter={StaticResource BooleanToHiddenVisibilityConverter}}" 
        Name="CommentTextBox"/>
</StackPanel>
like image 102
Tal Segal Avatar answered Nov 02 '22 04:11

Tal Segal


You can bind TextBox.Visiblity to CheckBox.IsChecked. If you want to toggle between Hidden and Visible then you need to either write custom IValueConverter or create simple Style.Trigger

<StackPanel>
    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" Name="CommentTextBox">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=CommentCheckBox, Path=IsChecked}" Value="True">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</StackPanel>

if you want to toggle between Collapsed and Visible there is an easier way and you can use build in BooleanToVisibilityConverter

<StackPanel>
    <StackPanel.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </StackPanel.Resources>
    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox 
        Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" 
        Visibility="{Binding ElementName=CommentCheckBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" 
        Name="CommentTextBox"/>
</StackPanel>
like image 30
dkozl Avatar answered Nov 02 '22 03:11

dkozl