To illustrate my point from the comments section, Say you have a UserControl
called WindowItem
that implements a Clone
method:
<UserControl x:Class="WpfApplication1.WindowItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Content="Click me"/>
</Grid>
</UserControl>
I create a class that will hold mutiple collections of WindowItem
public class MainWindowViewModel
{
public MainWindowViewModel()
{
}
public ObservableCollection<WindowItem> FirstCollection { get; set; }
public ObservableCollection<WindowItem> SecondCollection { get; set; }
public ObservableCollection<WindowItem> ThirdCollection { get; set; }
}
I also create a View
contaning three ListViews
, binded to my collections via Data Bindings
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<ListView ItemsSource="{Binding FirstCollection}"/>
<ListView ItemsSource="{Binding SecondCollection}"/>
<ListView ItemsSource="{Binding ThirdCollection}"/>
</StackPanel>
</Grid>
</Window>
Next, in the constructor of my MainWindow (known as View
), I set it's DataContext to be the class I created earlier (known as ViewModel
in MVVM).
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
Now, I can create cloning of my user controls for the different collection, shown in different lists in my view and register to the CollectionChanged
event of the first collection. Here is an example of doing so from the ViewModel constructor
public MainWindowViewModel()
{
FirstCollection = new ObservableCollection<WindowItem>();
SecondCollection = new ObservableCollection<WindowItem>();
ThirdCollection = new ObservableCollection<WindowItem>();
var windowItem = new WindowItem();
FirstCollection.Add(windowItem);
SecondCollection.Add(windowItem.Clone());
// Register to collection changes notifications
FirstCollection.CollectionChanged += FirstCollectionChanged;
}
FirstCollectionChanged
will fire whenever the first collection changes. You can "listen" for a removal action, and then remove the matching items from you other collections.
private void FirstCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
// Remove matching item from second and third collection.
}
}
You can test it by removing an item from the first collection
FirstCollection.RemoveAt(0);
Hope this helps