2011-09-14 17 views
2

안녕하세요, ListView 자체에서 ListView '소스를 업데이트하여 ListView가있는 다른 개체의 소스를 modifiy 할 필요가 없습니다. .I하지만 물론이WPF ListView ListView에서 소스 업데이트

무효 delete_Click (개체를 보낸 사람, RoutedEventArgs e)에 {

 List<EntityObject> items = new List<EntityObject>(); 

     foreach (EntityObject item in listView.SelectedItems) 
     { 
      itemsToBeDeleted.Add(item); 
     } 

     OnDeleteItems(items);//Database delete logic in here 

     listView.Items.Remove(items.First());//After DataBase delete also delete the item from the listview's source so that listView' itself will be updated too. 

     listView.Refresh(); 

     listView.SelectedItem = null; 
    } 

작동하지 않습니다있는 ListView에서 항목을 제거하여 원했고 나는 원본 목록을 통과하고 싶지 않아 ListView를 참조로 사용하고 ListView에서 다음과 같이 업데이트하십시오.

공개 무효 세트리스트 (REF 목록 itemList에) {

 sauce =itemlist; 
     list.ItemsSource = itemList; 

}


sauce.Items.Remove (items.First());

아이디어가 있으십니까?

답변

0

MVVM 디자인 패턴 사용에 대한 연구를 제안 할 수 있습니까? 이 작업을 수행하면이 시나리오와 다른 시나리오를 훨씬 더 깔끔하게 처리 할 수 ​​있습니다. 예를 들어 EntityObject Person (사용자가 EF를 사용한다고 가정)을 목록에 표시하고 버튼을 클릭하여 삭제한다고 가정 해 보겠습니다. 여기

<ListBox x:Name="lst" DisplayMemberPath="Name" SelectionMode="Extended"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="IsSelected" Value="{Binding IsSelected}"/> 
      </Style> 
     </ListBox.ItemContainerStyle>    
    </ListBox> 

, 당신은 당신이 목록 (DisplayMemberPath)에 표시 할 것을 지정하고이 사람의에 isSelected 속성에 목록 상자의 선택을 바인딩 : 처음에 우리는 목록을 보유 할 목록 상자를 생성합니다.

그 후, 우리는 가서이 목록 상자에 ObservableCollection<Person>을 결합 할 수있다 :

//in code behind constructor, but should actually be in a ViewModel 
var people = new ObservableCollection<Person> 
         { 
           new Person(false, "test1"), 
           new Person(true, "test2"), 
           new Person(true, "test3"), 
         }; 
lst.ItemsSource = people; 


//Person class for completeness 
public class Person 
{ 
    public Person(bool isSelected, string name) 
    { 
     IsSelected = isSelected; 
     Name = name; 
    } 

    public bool IsSelected { get; set; } 
    public string Name { get; set; } 
} 

하고 버튼을 클릭하면 다음 항목을 제거하는 케이크 한 조각이다 :

public void deleteClick(sender, args){ 
    var deleteme = people.Where(p => p.IsSelected).ToList(); 

    DoDeleteLogicInDB(deleteme); 

    deleteme.ForEach(p => people.Remove(p)); 
    //remove it from the observablecollection, and the view will automatically update. 
} 

나는 희망 이것은 올바른 방향으로 당신을 가리킬 것입니다!