How to change the visible property of TextBox and ListView from MVVM

Sarah 181 Reputation points

I have a ListView and a TextBox in my window. I want to set the Visibilty of these controls to Hidden. How do I do that from the MVVM?

<Window x:Class="Enum.Window1"
        Title="Window1" Height="450" Width="400">
        <StackPanel Orientation="Vertical">
            <ListView Width="300" Height="400" Visibility="{Binding LvIsVisible}">
                <ListView.View >
                    <GridView AllowsColumnReorder="False">
                        <GridViewColumn Header="Pro" Width="120">
                                    <TextBlock Text="{Binding ItemContent}" TextWrapping="Wrap"/>
            <TextBox Width="300" Height="25" Visibility="{Binding TxtBoxIsVisible}" />
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,372 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 41,631 Reputation points

    The VM should expose dependency properties indicating whether the UI element(s) should be visible or not (boolean). Then you bind the property to the visibility state of the control using XAML. Most likely you'll need to use the BooleanToVisibilityConverter in the XAML to convert the boolean value to/from Visibility but otherwise it should just work.

    0 comments No comments

  2. Hui Liu-MSFT 17,456 Reputation points Microsoft Vendor

    You could try to refer to the code below.


    using System;  
    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Globalization;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    using System.Windows.Data;  
    namespace ControlVisibleChangeByMVVM  
      public partial class MainWindow : Window  
        ViewModel vm=new ViewModel();  
        public MainWindow()  
      public class ViewModel: INotifyPropertyChanged  
        private bool myVisibility  =false;  
        public bool MyVisibility  
          get { return myVisibility; }  
          set { myVisibility=value; OnPropertyChanged("MyVisibility");}  
        ObservableCollection<Item> datas = new ObservableCollection<Item>();  
        public ObservableCollection<Item> Datas  
            return datas;  
        public ViewModel()  
          datas.Add(new Item("name1"));  
          datas.Add(new Item("name2"));  
          datas.Add(new Item("name3"));  
          datas.Add(new Item("name4"));  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void OnPropertyChanged([CallerMemberName] string name = null)  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
      public class BindingProxy : Freezable  
        public static readonly DependencyProperty DataProperty =  
           DependencyProperty.Register("Data", typeof(object),  
        public object Data  
          get { return GetValue(DataProperty); }  
          set { SetValue(DataProperty, value); }  
        protected override Freezable CreateInstanceCore()  
          return new BindingProxy();  
      public class Item  
        public string Name { get; set; }  
        public Item()  
        public Item(string name)  
          Name = name;  
      [ValueConversion(typeof(bool), typeof(Visibility))]  
      public sealed class BooleanToVisibilityConverter : IValueConverter  
        public bool IsReversed { get; set; }  
        public bool UseHidden { get; set; }  
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
          var val = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);  
          if (this.IsReversed)  
            val = !val;  
          if (val)  
            return Visibility.Visible;  
          return this.UseHidden ? Visibility.Hidden : Visibility.Collapsed;  
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
          throw new NotImplementedException();  

    The result:

    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 


    0 comments No comments