2012-04-30 3 views
3

사용자가 내 DataGrid 셀의 내용을 편집 할 때마다 알기를 원합니다. CellEditEnding 이벤트가 있지만 컬렉션에 변경 사항이 적용되기 전에 DataGrid가 바인딩되기 전에 호출됩니다.WPF DataGrid CellEditEnded 이벤트

내 데이터 격자는 ObservableCollection<Item>에 바인딩됩니다. Item은 클래스이며, WCF mex 끝점에서 자동으로 생성됩니다.

사용자가 컬렉션 변경 사항을 커밋 할 때마다 알 수있는 가장 좋은 방법은 무엇입니까?

UPDATE 내가 CollectionChanged 이벤트 해봤

, Item이 수정됩니다 때 트리거되지 않습니다 끝납니다.

답변

-1

CollectionChanged 이벤트에 이벤트 처리기를 추가하기 만하면됩니다.

코드 스 니펫 :

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

// ... 


    void ListCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     /// Work on e.Action here (can be Add, Move, Replace...) 
    } 

e.ActionReplace이며,이 목록의 목적은 대체되었다는 것을 의미합니다. 이 이벤트는 물론 변경 사항이 적용된 후에 트리거됩니다.

재미있게 보내십시오!

+0

사실 질문을 게시하기 전에 실제로 그렇게했습니다. 사용자가 컬렉션을 수정할 때 CollectionChanged 이벤트가 발생하지 않습니다. –

+0

이것은 사용자의 대답이 아니지만 CollectionChanged는 항목이 추가되거나 제거 된 경우에만보고합니다. 기회는 그리드이므로 컬렉션 자체를 변경하지 않고 위에서 언급 한 이벤트를 실행하지 않고 항목을 수정할 수 있습니다. – NestorArturo

+0

Woo, 네, 여기 오해입니다.'ItemChanged'는 전체 Item이 변경 될 때 (예 : 이전 Item 대신에 새로운 Item()을 넣을 때) 발생합니다. 모든 수정을 잡으려면'Item' 클래스가'INotifyPropertyChanged'를 구현해야합니다 :) – Damascus

0

편집 한 데이터 그리드 항목이 특정 컬렉션에 속하는지 여부를 알고 싶다면, 당신은 데이터 그리드의 RowEditEnding 이벤트 같은 것을 할 수있는 :

private void dg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) 
    { 
     // dg is the DataGrid in the view 
     object o = dg.ItemContainerGenerator.ItemFromContainer(e.Row); 

     // myColl is the observable collection 
     if (myColl.Contains(o)) { /* item in the collection was updated! */ } 
    } 
4

당신은 속성 멤버의 결합에 UpdateSourceTrigger=PropertyChanged을 사용할 수 있습니다 DataGrid에 대한. 그러면 CellEditEnding이 실행될 때 업데이트가 관찰 가능 컬렉션에 이미 반영됩니다.

<DataGrid SelectionMode="Single" 
      AutoGenerateColumns="False" 
      CanUserAddRows="False" 
      ItemsSource="{Binding Path=Items}" // This is your ObservableCollection 
      SelectedIndex="{Binding SelectedIndexStory}"> 
      <e:Interaction.Triggers> 
       <e:EventTrigger EventName="CellEditEnding"> 
       <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditStoryCommand}"/> // Mvvm light relay command 
       </e:EventTrigger> 
      </e:Interaction.Triggers> 
      <DataGrid.Columns> 
        <DataGridTextColumn Header="Description" 
         Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" /> // Name is property on the object i.e Items.Name 
      </DataGrid.Columns> 

</DataGrid> 

UpdateSourceTrigger =하여 PropertyChanged 즉시마다 대상 속성이 변경을 속성 소스를 변경합니다 아래

을 참조하십시오.

이렇게하면 관찰 대상 컬렉션 변경 이벤트에 이벤트 처리기를 추가하면 컬렉션의 개체를 편집 할 때 발생하지 않는 항목에 대한 편집 내용을 캡처 할 수 있습니다.

+0

고마워요! – Richard