2014-02-27 116 views
2

我對WPF數據綁定有點困惑。我已經嘗試了很多示例,但我認爲我不瞭解這個主題的基礎知識。WPF DataGrid綁定混淆

我有以下datagrid,綁定到一個ObservableCollection(Of T),其中T類有一個名稱屬性顯示在datagrid列中。我的T類還實現了INotifyPropertyChanged,並在Name屬性更改時正確觸發事件。

<DataGrid Grid.Row="1" Name="MyDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}" > 
    <DataGrid.Columns> 
     <DataGridTextColumn x:Name="NameColumn" Header="Name" Binding="{Binding Name}" /> 
    </DataGrid.Columns> 
</DataGrid> 

然後,在代碼隱藏類中,我有一個底層的數據網格集合。

public ObservableCollection<T> MyCollection 
{ 
    get; 
    set; 
} 

最後,當我的應用程序啓動時,我加載「MyCollection」屬性並告訴datagrid使用該集合。

public void InitApp() 
{ 
    MyCollection = [... taking data from somewhere ...]; 
    MyDataGrid.ItemsSource = MyCollection; 
} 

這一切都工作正常(數據顯示正常)。但是,如果我重新加載集合(從另一個地方再次獲取完全不同的數據),如果我不再執行MyDataGrid.ItemsSource = MyCollection;指令,數據網格不會更新。

我認爲每次重新載入數據時都使用XXX.ItemsSource = YYY不是一個好習慣,所以我猜我做錯了什麼。在一些例子中,我看到了XAML的DataGrid是綁定的,如:

<DataGrid ItemsSource="{Binding CollectionName}"> 
    ... 
</DataGrid> 

,我猜是針對使用該集合,因此,有沒有必要做.ItemsSource編程...但我不能讓它跑。

任何人都可以看到隧道盡頭的燈光嗎?

回答

1

您可以通過數據綁定DataGrid的ItemsSource而不是手動分配它。簡單地聲明MyCollection作爲公共財產,適當提高PropertyChanged通知,並設置DataContextDataGrid(或設置DataContext爲窗口,在DataGrid所在):

private ObservableCollection<MyClass> _myCollection 
public ObservableCollection<MyClass> MyCollection 
{ 
    get { return _myCollection; } 
    set 
    { 
     _myCollection = value; 
     NotifyPropertyChanged("MyCollection"); 
    } 
} 

public void InitApp() { 
    MyCollection = [... taking data from somewhere ...]; 
    MyDataGrid.DataContext = this; 
} 

<DataGrid ItemsSource="{Binding MyCollection}"> 
    ... 
</DataGrid> 

UPDATE:

你的方法也宣告結合在XAML中:

<DataGrid Grid.Row="1" Name="MyDataGrid" 
         AutoGenerateColumns="False"       
         ItemsSource="{Binding}" > 

而且應該沒有設置ItemsSource但設置它的DataContext的集合:每當收集重載,手動設置ItemsSource時可以」

public void InitApp() { 
    MyCollection = [... taking data from somewhere ...]; 
    MyDataGrid.DataContext = MyCollection; 
} 

理論上(因爲我沒有具體試過情景在這個問題上),與ItemsSource結合您的DataGrid會自動更新t帶來同樣的效果。

+0

你將不得不執行INotifyPropertyChanged以提高PropertyChanged事件。如果您正在使用ObservableCollection,那麼您無需爲提高事件而煩惱。只需將MyCollection = new ObservableCollection ();在您的構造函數中,然後將在您的InitApp方法中加載的項目添加到MyCollection中。這一切都假設您在數據網格中使用以下綁定:ItemsSource =「{Binding MyCollection}」 – Bijington

0

你沒有解釋最重要的部分是你如何實現填充?如果通過將新集合引用分配給舊變量來實現,那麼它將無法工作,因爲集合是引用類型解決問題的任何方法您都可以執行以下兩項操作之一(在您的解決方案旁邊): 首先是將ObservableCollection定義爲窗口中的Dependency屬性,然後可以使用它。 第二,最簡單的就是清除集合,然後添加使用foreach循環,從源頭收集 這裏的新項目是第一種情況的例子:

public static readonly DependencyProperty MyCollectionProperty = DependencyProperty.Register("MyCollection", typeof(ObservableCollection<T>), typeof(MainWindow)); 

public ObservableCollection<T> MyCollection 
{ 
    get 
    { 
     return this.GetValue(MyCollectionProperty) as string; 
    } 
    set 
    { 
     this.SetValue(MyCollectionProperty, value); 
    } 
} 

//assign the collection value at any time 
MyCollection = ...... 

//you can bind it as the past