10

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.

1
  • I think you will want to first bind the CheckBox to a property on your view model, then you would create a Converter to convert your boolean property value into a visibility enum. Commented Oct 29, 2014 at 10:45

2 Answers 2

23

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.