2009-11-20 5 views
1

잠시 동안이 패턴을 구현하지 않았으며 (3 번과 반대로 2 번에 구현했을 때), 모든 패턴이 곧은 것처럼 보인 몇 가지 예가 있지만 나는 코드의 아래 부분에서 잘못 한 일을 해결할 수 없습니다 (항목이 업데이트되지 않은 경우 속성 이벤트가 발생) :Silverlight : INotifyPropertyChanged가 작동하지 않는 것 같습니다.

public partial class Index : Page 
{ 
    private IndexViewModel _vm; 

    public Index() 
    { 
     InitializeComponent(); 
     _vm = new IndexViewModel(19);   

     this.TheDataGrid.ItemsSource = _vm.Rows; 
    } 




public class IndexViewModel : INotifyPropertyChanged 
    { 
    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 

     protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
     {   
      this.PropertyChanged(this, e); 
     } 

public SortableCollectionView Rows 
     { 
      get 
     { 
      return _rows; 
     } 
     set 
     { 
      if (_rows == value) 
       return; 

      _rows = value; 
      this.OnPropertyChanged(new PropertyChangedEventArgs("Rows"));         
     } 
    } 

이 내 데이터 그리드를 새로 고침하지 않습니다 ...에 '해킹'으로 DataGrid 객체를 내 viewmodel에 전달하고 거기에 바인딩해야합니다.

public IndexViewModel(int containerModelId, DataGrid shouldNotNeed) 
     { 
      ContainerModelId = containerModelId; 

     LoadOperation<vwColumn> headings = _ttasContext.Load(_ttasContext.GetRecordColumnsQuery(ContainerModelId)); 
     headings.Completed += (sender2, e2) => 
     { 
      //load data 
      LoadOperation<vwDataValue> data = _ttasContext.Load(_ttasContext.GetRecordsQuery(ContainerModelId, null)); 
      data.Completed += (sender3, e3) => 
      { 

        Rows = FormatData(data, headings); 
shouldNotNeed.ItemsSource = Rows; 
       }; 
      }; 
     } 

답변

2

할당 _vm.Rows TheDataGrid.ItemsSource는 변경 알림 콜백을 자동으로 연결하지 않습니다. 이 시도 : 을 XAML에서 : 코드에서

<... x:Name=TheDataGrid ItemsSource={Binding Rows}> 

: Codism으로

this.DataContext = _vm; 
0

이 주요 문제를 지적하는 것은 당신이 INotifyPropertyChanged을 활용하는 바인딩을 사용 할 필요가있다. 그러나 나는이 구현 패턴을 추천 할 것입니다 : -

public event PropertyChangedEventHandler PropertyChanged; 

void NotifyPropertyChanged(string name) 
{   
    if (PropertyChanged != null) 
    PropertyChanged(this, new PropertyChangedEventArgs(name); 
} 

...이 방법은 특성이 관찰되고되지 않는 개체 인스턴스에 미치는 영향을 최소화하는 것이

set 
    { 
    if (_rows != value) 
    { 
     _rows = value; 
     NotifyPropertyChanged("Rows"); 
    } 
    } 

참고. 원래 패턴에서는 PropertyChangedEventArgs의 인스턴스를 만들고 실제로 수신 중인지 여부에 관계없이 이벤트 대리자를 호출합니다.

+0

난 당신이 선언의 끝에 = 대리자를 {}가 있다면 경우에 당신이이 필요하지 않았다 어딘가 다른 게시물을 읽을 수 있습니다 (속성이 변경! = null이) 그러나, 경우 그 경우 코드가 덜 명확하다는 사실을 주석으로 처리하므로 다시 변경했습니다. 감사합니다! –

+0

@ Grayson : 그렇습니다. 위임 {} 트릭에는 이점이 있습니다. 그러나 일반적인 이벤트 패턴은 적절한 EventArgs 개체를받는 가상 보호 된 메서드 인 "OnXXXX"를 사용하는 것입니다. INotifyPropertyChanged에 대한이 접근법의 문제점은 속성 값이 빠르게 바뀔 수 있다는 것입니다 (예를 들어 움직이는 경우). 이로 인해 방대한 양의 PropertyChangedEventArgs 인스턴스가 생성 될 수는 있지만 리스닝이 없으므로 GC에 불필요한 부담이 생깁니다. – AnthonyWJones

0

this.TheDataGrid.ItemsSource = _vm.Rows

집합이 소스 INotifyCollectionChanged를 구현하는 경우 수집에 대한 변경은 데이터 격자에 의해 관찰 될 수있다하는 DataGird의 ItemsSource로 할당

.
코드 샘플에서 SortableCollectionView 형식이 INotifyCollectionChanged를 구현하는지 ObservableCollection에서 상속되는지 여부를 알 수 없습니다.
INotifyCollectionChanged를 구현하면 속성 행에 대해 배킹 필드 _rows를 재설정 할 수 없으므로 컬렉션의 항목을 지우고 필요에 따라 추가 할 수 있습니다.
희망이

관련 문제