2014-03-13 126 views
0

我必須在按鈕單擊時在網格中顯示Observable Collection的內容。在按鈕上單擊(我知道可以使用Command綁定,但爲簡單起見,單擊處理程序)集合應該填充並顯示來自模型類的名稱屬性。將CustomClass的ObservableCollection綁定到網格

視圖模型:

public class Sample 
    { 
     public ObservableCollection<SchoolModel> ModelCollection { get; set; } 

     public void GridMethod() 
     { 
      ModelCollection = new ObservableCollection<SchoolModel>(); 
      ModelCollection.Add(new SchoolModel() {Id=1, Name="ABC" }); 
      ModelCollection.Add(new SchoolModel() { Id = 2, Name = "PQR" }); 
      ModelCollection.Add(new SchoolModel() { Id = 3, Name = "DEF" }); 
     } 
    } 

型號:

public class SchoolModel : INotifyPropertyChanged 
    { 
     private int id; 
     private string name; 

     public int Id 
     { 
      get 
      { 
       return id; 
      } 
      set 
      { 
       id = value; 
       OnPropertyChanged("Id"); 
      } 
     } 

     public string Name 
     { 
      get 
      { 
       return name; 
      } 
      set 
      { 
       name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = this.PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

查看:

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <Button Content="Click" Click="Button_Click" Grid.Row="0" Grid.Column="0"/> 

     <ItemsControl ItemsSource="{Binding ModelCollection}" Grid.Row="0" Grid.Column="1" > 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Rows="{Binding Path=ModelCollection.Count}" Columns="{Binding Path=ModelCollection.Count}" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate > 
        <TextBlock Width="auto" Height="auto" Text="{Binding Path=Name}"/>      
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 

任何想法?爲什麼Collection沒有顯示在網格中

+0

您設置了<'這將導致所有項目顯示在單行中。你不想要「Count」或類似的平方根嗎? – dkozl

+0

我想以格式顯示結果(3 * 3)等 – user2323308

回答

0

在Sample class的構造函數中初始化集合,否則在加載時綁定將失敗,直到您手動爲ModelCollection提升PropertyChanged事件爲止,它將不起作用。

或者:

public Sample() 
{ 
    ModelCollection = new ObservableCollection<SchoolModel>(); 
} 

OR

落實Sample類INPC以及在GridMethod()提高初始化邏輯後PropertyChanged事件。

public void GridMethod() 
    { 
     ModelCollection = new ObservableCollection<SchoolModel>(); 
     ModelCollection.Add(new SchoolModel() {Id=1, Name="ABC" }); 
     ModelCollection.Add(new SchoolModel() { Id = 2, Name = "PQR" }); 
     ModelCollection.Add(new SchoolModel() { Id = 3, Name = "DEF" }); 
     OnPropertyChanged("ModelCollection"); 
    } 
+0

無法獲取OnPropertyChanged(「ModelCollection」)。編譯錯誤OnPropertyChanged在當前上下文中不存在 – user2323308

+0

顯然,您需要像在SchoolModel類中那樣在類中添加方法。從INPC中導出並創建方法。 –

+0

@ user2323308 - 這是否適合您? –

相關問題