You could try to refer to the code below.
MainWindow.xaml:
MainWindow.xaml.cs:
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()
{
InitializeComponent();
DataContext=vm;
}
}
public class ViewModel: INotifyPropertyChanged
{
private bool myVisibility =false;
public bool MyVisibility
{
get { return myVisibility; }
set { myVisibility=value; OnPropertyChanged("MyVisibility");}
}
ObservableCollection- datas = new ObservableCollection
- ();
public ObservableCollection
- Datas
{
get
{
return datas;
}
set
{
datas=value;
OnPropertyChanged("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),
typeof(BindingProxy));
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:
![196026-55.gif][1]
****************************************************************************
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.**
[5]: https://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.html
[1]: /answers/storage/attachments/196026-55.gif