1

我希望这很简单,只是错过了一些明显的东西。我正在使用 MVVM 并有一个Datagrid绑定到 a 的CollectionViewSourcethis 又填充了一个ObservableCollectionObservableCollection最初未填充并通过 UI 上的复选框添加到

我遇到的问题是,当ObservableCollection添加到时,标题出现在分组上,DataGrid但各个行本身却没有。

任何帮助真的很感激,

这是我的 Datagrid 的 XAML

<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"));

为了确认可观察的集合CollectionView在 VM 中更新得很好,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");
    }
}
4

2 回答 2

2

我将以下内容添加到我的 XAML 中,结果发现我错过了一些小东西,但是查看了多个使用 CollectionViewSource 并在数据网格中分组的示例,我只发现微软将其包含为 GroupStyle 的一部分

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

希望这可以帮助任何有类似问题的人

于 2013-01-22T11:38:23.403 回答
0
public ObservableCollection<Booking> _bookings{get;set;}

绑定源必须是属性而不是字段。因为绑定系统使用反射,它只查找属性而不是字段。我希望这会有所帮助。

于 2013-01-16T15:05:24.417 回答