wisey
wisey

Reputation: 76

WPF Grouped Datagrid rows not visible when added to Collection

I am hoping that this is something simple and have just missed something obvious. I am using MVVM and have a Datagrid which is bound to a CollectionViewSource this in turn is populated with an ObservableCollection, the ObservableCollection is initally unpopulated and added to by way of tick boxes on the UI

The problem I have is that when the ObservableCollection is added to, the Headers appear for the grouping on the DataGrid but the individual rows themselves don't.

Any help really appreciated,

Here is my XAML for the 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>

And my code for the Collections

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

To confirm the observable collection is updating fine as is the CollectionView in the VM, the ItemCount in the header even increases in the UI I just can't seem to make the rows appear.

Thanks in advance

Edit:

I have changed my code to assign directly to Bookings as opposed to _bookings as per EthicalLogics suggestion but this hasn't helped Bookings is defined as below:

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

}

Here is GroupedBookings

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

Upvotes: 1

Views: 1228

Answers (2)

wisey
wisey

Reputation: 76

I added the following to my XAML, turns out I had missed something small however having looked at multiple examples of using a CollectionViewSource and grouping in a data grid I only found the microsoft one to contain this as part of the GroupStyle

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

Hope this helps anyone that has a similar issue

Upvotes: 2

yo chauhan
yo chauhan

Reputation: 12305

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

Binding Source must be a property not field. Because binding system uses reflection and it looks only for properties not fields.I hope this will help.

Upvotes: 0

Related Questions