I'm assuming you have one collection for the combobox, and one for the listbox.
In your listbox collection item, add a property called IsVisible.
When your combobox selection changes, set your new IsVisible property to true or false, depending on whether or not the item is associated with what was selected in the combobox.
I derive all of my MVVM items from a base class called
Notifiable
. This class implements the
INotifyPropertyChanged
and
IDataErrorInfo
interfaces, and contains several properties that are useful in a WPF viewmodel. I've included it here in case you can make use of it. If you want to see it in use, Check out my
SQLXAgent - Jobs for SQL Express[
^] article. All of the UI is WPF, and I use my
Notifiable
class extensively.
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfCommon
{
public class Notifiable : INotifyPropertyChanged, IDataErrorInfo
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
if (propertyName != "IsModified")
{
this.IsModified = true;
}
}
}
#endregion INotifyPropertyChanged
#region IDataErrorInfo Code
public string Error
{
get { return "Error"; }
}
public string this[string columnName]
{
get
{
return Validate(columnName);
}
}
protected virtual string Validate(string propertyName)
{
string validationMsg = string.Empty;
return validationMsg;
}
#endregion IDataErrorInfo Code
#region Fields
private bool isModified;
private bool isSelected;
private bool isVisible;
private bool isEnabled;
private bool isExpanded;
private bool isNew;
private bool isClone;
private bool isDeleted;
private bool isValid;
#endregion Fields
#region Properties
public virtual bool IsModified
{
get { return this.isModified; }
set
{
if (this.isModified != value)
{
this.isModified = value;
this.NotifyPropertyChanged("IsModified");
}
}
}
public virtual bool IsSelected
{
get { return this.isSelected; }
set
{
if (this.isSelected != value)
{
this.isSelected = value;
this.NotifyPropertyChanged("IsSelected");
}
}
}
public virtual bool IsExpanded
{
get { return this.isExpanded; }
set
{
if (this.isExpanded != value)
{
this.isExpanded = value;
this.NotifyPropertyChanged("IsExpanded");
}
}
}
public virtual bool IsVisible
{
get { return this.isVisible; }
set
{
if (value != this.isVisible)
{
this.isVisible = value;
this.NotifyPropertyChanged("IsVisible");
}
}
}
public virtual bool IsEnabled
{
get { return this.isEnabled; }
set
{
if (value != this.isEnabled)
{
this.isEnabled = value;
this.NotifyPropertyChanged("IsEnabled");
}
}
}
public virtual bool IsNew
{
get { return this.isNew; }
set
{
if (value != this.isNew)
{
this.isNew = value;
this.NotifyPropertyChanged("IsNew");
}
}
}
public virtual bool IsClone
{
get { return this.isClone; }
set
{
if (value != this.isClone)
{
this.isClone = value;
this.NotifyPropertyChanged("IsClone");
}
}
}
public virtual bool IsDeleted
{
get { return this.isDeleted; }
set
{
if (value != this.isDeleted)
{
this.isDeleted = value;
this.NotifyPropertyChanged("IsDeleted");
}
}
}
public virtual bool IsValid
{
get { return this.isValid; }
set
{
if (value != this.isValid)
{
this.isValid = value;
this.NotifyPropertyChanged("IsValid");
}
}
}
#endregion Properties
#region Constructor
public Notifiable(bool isNew=false)
{
this.IsNew = isNew;
this.IsModified = false;
this.IsVisible = true;
this.IsEnabled = true;
this.isValid = true;
this.IsClone = false;
this.IsDeleted = false;
}
#endregion Constructor
}
}