0

メインを「リッスン」したいのですが、ListView1 つ以上のアイテムが削除されると、他ListViewの s のクローンも削除されます。

例:
メインに 5 つのアイテムがあります。2ListView
番目ListViewには 2 つのクローン アイテムがあります (メインでの位置ListViewは 0 と 2)
3 番目ListViewには別の 2 つのクローン アイテムがあります (メインの `ListView での位置は 0 と 4 です) )

main からアイテム番号 0 を削除すると、他の s のListViewすべてのクローンから削除されます。ListView

今、私がやっていることは、 item が main から削除されたときにListView、他の s をループしてListView、削除されたものと一致するかどうかを確認し、一致する場合はそれらも削除します。

について読みましたObservableCollectionが、自分のニーズに合わせて実装する方法がわかりません...その方法を簡単に説明するヘルプ/簡単なガイドをいただければ幸いです。

編集:
これは WinForms ソリューションです-WPF ソリューションに変換する方法がわかりません。さらに、これが必要かどうかもわかりません
http://www.codeproject.com/Articles/4406/An-Observer-Pattern- and-an-Extended-ListView-Event

4

2 に答える 2

2

コメント セクションからの私の要点を説明するために、メソッドを実装するUserControl呼び出しがあるとします。WindowItemClone

<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>

複数のコレクションを保持するクラスを作成しますWindowItem

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {

    }

    public ObservableCollection<WindowItem> FirstCollection { get; set; }

    public ObservableCollection<WindowItem> SecondCollection { get; set; }

    public ObservableCollection<WindowItem> ThirdCollection { get; set; }
}

また、データバインディングを介してコレクションにバインドされた、含まれるViewthreeを作成しますListViews

<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>

次に、MainWindow のコンストラクター ( として知られているView) で、そのDataContextを以前に作成したクラス ( MVVMViewModelで知られている) に設定します。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
    }
}

これで、別のコレクションのユーザー コントロールのクローンを作成し、ビュー内の別のリストに表示してCollectionChanged、最初のコレクションのイベントに登録できます。ViewModel コンストラクターからこれを行う例を次に示します。

    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最初のコレクションが変更されるたびに発生します。削除アクションを「リッスン」してから、一致するアイテムを他のコレクションから削除できます。

     private void FirstCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            // Remove matching item from second and third collection.
        }
    }

最初のコレクションからアイテムを削除することでテストできます

FirstCollection.RemoveAt(0);

お役に立てれば

于 2013-10-06T19:15:32.217 に答える
1

ListView オブジェクトにアイテムを直接追加および削除する代わりに、それを何らかのコレクション (できれば Observable Collection) にバインドし、ObservableCollection からアイテムを追加/削除すると、UI にも反映されます。

例えば

ObservableCollection<WindowItem> MyCollection=new ObservableCollection<WindowItem>();

次に、アイテムを追加します

MyCollection.Add(new WindowItem(parameters));

そして、複製されたリストビューからアイテムを表示するには、次のようにメインの ObservableCollection にイベントハンドラーを追加します

MyCollection.CollectionChanged+=new System.Collections.Specialized.NotifyCollectionChangedEventHandler(CollectionChanged);

void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    //bind your another listviews to cloned items
}
于 2013-10-06T19:14:43.337 に答える