I was thinking of using a converter where we can convert the ItemsSource binding to each listbox.
But the tricky part is that everytime you add/remove, you should raise a propertychange event to trigger the converter. I have here a dirty solution.
Here's the viewmodel, I added a ClickCommand so that we can test the adding part.
public class TagVM: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
ObservableCollection<Tag> tagCollection;
public TagVM()
{
tagCollection = new ObservableCollection<Tag>();
tagCollection.Add(new Tag() { Name = "two", Priority = "2" });
tagCollection.Add(new Tag() { Name = "another 2", Priority = "2" });
tagCollection.Add(new Tag() { Name = "three", Priority = "3" });
tagCollection.Add(new Tag() { Name = "one", Priority = "1" });
}
public ObservableCollection<Tag> TagCollection
{
get { return tagCollection; }
set
{
tagCollection = value;
RaisePropertyChanged("TagCollection");
}
}
public ICommand ClickCommand
{
get
{
return new RelayCommand(() => { tagCollection.Add(new Tag() { Name = "another 3 ", Priority = "3" }); RaisePropertyChanged("TagCollection"); });
}
}
}
Here's the converter where we use the parameter in the where part:
public class ItemsConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
ObservableCollection<Tag> oval = value as ObservableCollection<Tag>;
if (oval != null)
{
return oval.Where(x => x.Priority == parameter.ToString()).ToList();
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
And lastly, here's the view where we bind the items source, used the itemconverter and a converterparameter:
<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="350" Width="525">
<stackpanel>
<stackpanel.resources>
<local:itemsconverter x:key="itemConverter" xmlns:local="#unknown" />
</stackpanel.resources>
<listbox itemssource="{Binding TagCollection, Converter={StaticResource itemConverter}, ConverterParameter=1}">
<listbox.itemtemplate>
<datatemplate>
<textblock text="{Binding Name}" />
</datatemplate>
</listbox.itemtemplate>
</listbox>
<listbox itemssource="{Binding TagCollection, Converter={StaticResource itemConverter}, ConverterParameter=2}">
<listbox.itemtemplate>
<datatemplate>
<textblock text="{Binding Name}" />
</datatemplate>
</listbox.itemtemplate>
</listbox>
<listbox itemssource="{Binding TagCollection, Converter={StaticResource itemConverter}, ConverterParameter=3}">
<listbox.itemtemplate>
<datatemplate>
<textblock text="{Binding Name}" />
</datatemplate>
</listbox.itemtemplate>
</listbox>
<button content="Add Another 3">
Command="{Binding ClickCommand}" />
</button></stackpanel>
</window>
I hope that helped. :)