2013-01-16 30 views
1

我希望這是簡單的東西,只是錯過了明顯的東西。我使用MVVM並且具有綁定到CollectionViewSource這又填充了一個ObservableCollection一個Datagrid,在ObservableCollection是initally無人居住,並加入到由勾選框的方式對UIWPF分組Datagrid行添加到集合時不可見

我的問題是,當ObservableCollection被添加到,標題出現在DataGrid上的分組中,但是單獨的行本身不包含。

任何幫助非常感激,

這是我的XAML爲DataGrid

<DataGrid DataContext="{Binding GroupedBookings}" 
      ItemsSource="{Binding SourceCollection}" 
      AutoGenerateColumns="False" 
      SelectionMode="Single" 
      SelectionUnit="FullRow" 
      CanUserSortColumns="True" 
      SelectedItem="{Binding SelectedBooking}" 
      CanUserAddRows="False"> 
    <DataGrid.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.HeaderTemplate> 
     <DataTemplate> 
      <StackPanel> 
      <TextBlock Text="{Binding Path=MemberCount.SupporterType}" 
         FontWeight="Bold" 
         Padding="3" /> 
      </StackPanel> 
     </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
     <GroupStyle.ContainerStyle> 
     <Style TargetType="{x:Type GroupItem}"> 
      <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
       <Expander> 
        <Expander.Header> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding Path=Name}" /> 
         <TextBlock Text="{Binding Path=ItemCount}" 
            Margin="8 0 4 0" /> 
        </StackPanel> 
        </Expander.Header> 
       </Expander> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter> 
     </Style> 
     </GroupStyle.ContainerStyle> 
    </GroupStyle> 
    </DataGrid.GroupStyle> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="Cost" 
         Binding="{Binding Cost}" /> 
    <DataGridTextColumn Header="Order No" 
         Binding="{Binding LinkedOrderID}" /> 
    </DataGrid.Columns> 
</DataGrid> 

和我的收藏

_bookings = new ObservableCollection<Booking>(rep.Bookings_Get().Where(x => x.JobID == CurrentJob.JobID)); 
GroupedBookings = CollectionViewSource.GetDefaultView(Bookings); 
GroupedBookings.GroupDescriptions.Add(new PropertyGroupDescription("MemberCount.SupporterType")); 

代碼,以確認觀察集合正在更新罰款是VM中的CollectionView,頭中的ItemCount甚至在UI中增加,我似乎無法使行顯示。

在此先感謝

編輯:

我已經改變了我的代碼直接分配給Bookings而不是_bookings按照EthicalLogics建議,但是這並沒有幫助Bookings定義如下:

public ObservableCollection<Booking> Bookings 
{ 
    get { return _bookings; } 
    set 
    { 
     _bookings = value; 
     OnPropertyChanged("Bookings"); 
    } 

} 

這裏是GroupedBookings

public ICollectionView GroupedBookings 
{ 
    get { return _groupedBookings; } 
    set 
    { 
     _groupedBookings = value; 
     OnPropertyChanged("GroupedBookings"); 
    } 
} 
+0

可以請你展示,你定義_bookings,因爲它似乎是一個字段中未從C#Codeing公約 – ethicallogics

+0

私人的ObservableCollection _bookings財產;這是在我的虛擬機中定義的。謝謝 – wisey

+0

我已經排除它與itemsource的問題,如果我註釋掉Groupstyle xaml它更新正確,因此只能假設我的xaml在組風格中存在問題... – wisey

回答

1

我將以下內容添加到我的XAML中,結果發現我錯過了一些小事,但是查看了使用CollectionViewSource和數據網格中的分組的多個示例,我只找到了包含此作爲GroupStyle的一部分的這個微軟的一個例子

<Expander.Content> 
    <ItemsPresenter /> 
</Expander.Content> 

希望這有助於,有一個類似的問題,任何人

0
public ObservableCollection<Booking> _bookings{get;set;} 

綁定源必須是屬性而非字段。因爲綁定系統使用反射,它看起來只屬性而不是fields.I希望這會有所幫助。

+0

不幸的是,這並沒有幫助, _bookings被分配給,但作爲預訂get的一部分返回,並且是我的CollectionViewSource中使用的。我爲所有綁定元素定義了屬性。再次感謝Chris – wisey

+0

然後將值賦予預訂而不是_bookings,因爲當您將值分配給_bookings時,它將不會在UI上通知。但是如果你將它分配給預訂,它將被通知。 – ethicallogics

+0

再次感謝您對此的幫助,我剛剛給出了一個解決方案,並且它沒有區別,因爲您的信息我已編輯我的問題以包括預訂代碼,因爲您可以看到我使用的是觸發OnPropertyChanged並切換到一個collectionviewsource它的工作很好從預訂ObservableCollection – wisey

相關問題