2011-01-07 2 views
0

WPF 목록의 변화를 통지하고, 데이터 그리드를 업데이트하지만, 나는 어떤 속성을 통보해야 내가 모르는 뭔가가있어 보인다?나는 아래와 같은 목록에 광고 항목에 노력하고있어 .net4.0

C#을

public partial class Window8 : INotifyPropertyChanged 
    { 
     public TestObj TestObject { get; set; } 
     public int Count { get; set; } 

     public Window8() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      var newList = new List<Test>(); 

      newList.Add(new Test{I = 1, S = "Test"}); 
      TestObject = new TestObj { S = "Testing object", List = newList }; 

      Count = TestObject.List.Count; 
     } 


     private ICommand _addItemCommand; 
     public ICommand AddItemCommand 
     { 
      get 
      { 
       if (_addItemCommand == null) 
        _addItemCommand = new RelayCommand(n => 
                  { 
                   TestObject.List.Add(new Test {I = 1, S = "New object"}); 
                   Count = TestObject.List.Count; 

                   NotifyPropertyChanged("TestObject"); 
                   //NotifyPropertyChanged("TestObject.List"); 

                   NotifyPropertyChanged("Count"); 

                  }); 
       return _addItemCommand; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 


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

    public class TestObj 
    { 
     public string S { get; set; } 
     public List<Test> List { get; set; } 
    } 

    public class Test 
    { 
     public string S { get; set; } 
     public int I { get; set; } 
    } 

XAML

<StackPanel> 
     <Button Height="23" Width="100" Command="{Binding Path=AddItemCommand}" >Add Item</Button> 
     <DataGrid Height="100" ItemsSource="{Binding Path=TestObject.List}" IsReadOnly="True" /> 
<TextBlock Text="{Binding Path=Count}" /> 

나는이 목록에 개체를 추가 버튼을 눌러

, 카운터는 카운트 업하지만 아무것도 목록에 나타나지 않습니다.

답변

3

public Window8() 
{ 
    InitializeComponent(); 
    DataContext = this; 

    var newList = new ObservableCollection<Test>(); 

    newList.Add(new Test { I = 1, S = "Test" }); 
    TestObject = new TestObj { S = "Testing object", List = newList }; 

    Count = TestObject.List.Count; 
} 

public class TestObj 
{ 

    public string S { get; set; } 
    public ObservableCollection<Test> List { get; set; } 
} 

또한 창에없는 객체에 구현되어야에서 INotifyPropertyChanged

0

개체는 INotifyPropertyChanged이 아니고 양식이나 창을 구현해야합니다.

당신은

코드 BE-해야 대신 목록의 ObservableCollection에 사용해야

관련 문제