2016-05-30 6 views
0

からのListViewItemの選択を解除します。ユーザが最初にリストビューから項目をクリックしたときには、第二のリストビューに追加され、彼は最初のリストビューからアイテムを選択解除するときには、第二のリストビューから削除されます。は、最初はMultipleSelection有効ListViewコントロールである私は2つのリストビューを持っているViewModelに

私の問題は、今私は、ユーザーが第二のListView(すでに実装されている)からのListViewItemを削除し、アイテムがfirstListViewに選択解除されていことができるようにしたいです。私は何の問題は、単に最初のListViewで

<ListView.ItemTemplate> 
       <DataTemplate > 
        <RelativePanel > 
         <TextBlock Text="{Binding Name}" RelativePanel.LeftOf="MoveButton"/> 
         <Button Name="MoveButton" Command="{Binding ElementName=NaughtySelectionList, Path=DataContext.MoveCommand}" 
           CommandParameter="{Binding Id}" Content="+" 
           Style="{StaticResource ButtonClearStyle}" 
           RelativePanel.AlignRightWithPanel="True" 
           RelativePanel.AlignTopWithPanel="True"/> 
        </RelativePanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 

     </ListView> 
     <ListView Name="NaughytSelectedList" ItemsSource="{Binding SelectedNaughtyChildren}"> 
      <ListView.ItemTemplate> 
       <DataTemplate > 
        <RelativePanel > 
         <TextBlock Text="{Binding Name}" RelativePanel.LeftOf="MoveBackButton" /> 
         <Button Name="MoveBackButton" 
           Command="{Binding ElementName=NaughytSelectedList, Path=DataContext.MoveBackCommand}" 
           CommandParameter="{Binding Id}" Content="✖" 
           Style="{StaticResource ButtonClearStyle}" 
           RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignTopWithPanel="True" /> 
        </RelativePanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

のViewModel

private Child selectedChild ; 
    public Child SelectedChild 
    { 
     get { return selectedChild; } 
     set { selectedChild = value; 


      base.RaisePropertyChanged(); 
     } 
    } 


    private DelegateCommand<IList<Object>> selectionChangedCommand; 
    public DelegateCommand<IList<Object>> SelectionChangedCommand 
     => selectionChangedCommand ?? (selectionChangedCommand = new DelegateCommand<IList<object>>(AddToNaughty, CanAddToNaughty)); 
    private bool CanAddToNaughty(IList<object> arg) 
    { 
     return true; 

    } 

    private void AddToNaughty(IList<object> currentSelection) 
{ 

     var currentSelectionCollection= new ObservableCollection<Child>(); 

     foreach (var child in currentSelection) 
     { 
      currentSelectionCollection.Add(child as Child); 
     } 

     foreach (var child in currentSelectionCollection) 
     { 
      if (!SelectedNaughtyChildren.Contains(child)) 
       SelectedNaughtyChildren.Add(child); 
     } 


     //Check if an item has been deselected. 
     var copyOfSelectedNaughtyChildren = new ObservableCollection<Child>(SelectedNaughtyChildren); 
      var deselectedItems = from child in copyOfSelectedNaughtyChildren 
          where !currentSelectionCollection.Contains(child) 
          select child; 


     //Remove the DeselectedItem. 
     foreach (var child in deselectedItems) 
     { 
      SelectedNaughtyChildren.Remove(child); 
     }  
    } 

    private DelegateCommand<int> moveBackCommand; 
    public DelegateCommand<int> MoveBackCommand 
     => moveBackCommand ?? (moveBackCommand = new DelegateCommand<int>(DeleteChild, CanDeleteChild)); 



    private bool CanDeleteChild(int arg) 
    { 
     return true; 
    } 

    private void DeleteChild(int id) 
    { 
     var childMatch = from child in SelectedNaughtyChildren 
         where child.Id == id 
         select child; 

     var childToRemove = childMatch.First(); 
     if (childToRemove != null) 
      SelectedNaughtyChildren.Remove(childToRemove); 
     //Hear I want to deselect the item from the first ListView 
     // setting selected item to null deselects all the items. 
     SelectedChild = null; 

    } 
} 

編集をその項目の選択を解除する方法を必要としない項目を削除することができます。私は私が望む機能を表示するには、いくつかの画像を追加しました。私はこれが働いているが、コードの背後でしか使用していない。可能であればViewModelでこれを実現したいと思います。あなたがここfirstlistviewSelectedItemsリストから項目を削除する必要が

So the user selects some items and they appear in the ListView to the right

When the user deletes and item from the right it gets deselected in the left

+0

は、ソリューションの仕事をしたのですか? – Archana

+0

いいえ、残念ながら私が見つけた唯一の修正は、背後にあるコードからlistView.SelectedItemsを更新しました。私はViewModelには、リストビューselectedItemsのを更新するための論理的な方法を見ることはできません。 observableCollectionから項目を削除すると、その項目はバインディングListViewから削除されます。 –

+0

私はそれがどのように動作するかを示すいくつかの画像で投稿を更新しました –

答えて

0

はコード

foreach(var item in FirstListView.Items)//or use firstlistview observable collection 
{ 
//Compare item to be deselected 
    if(childToRemove == item) 
    FirstListView.SelectedItems.Remove(item); 
} 
+0

残念ながら、ObservableCollectionのItemプロパティは選択できません。 –

+0

Observablecollectionでアイテムプロパティを選択するつもりはありませんでした。 observableコレクションを直接使用するか、ListViewにアクセスしてItemsプロパティを取得できる場合 – Archana

関連する問題