1

DataGrid绑定了一个ListCollectionView包含一些使用ListCollectionView. 如果我不添加GroupDescription(因此不应将任何项目分组),则项目将正确显示。但是,当我设置 时GroupDescription,我可以看到每组项目的名称和数量,但不显示项目本身。

有人可以指出我在这里做错了什么吗?

项目视图.xaml

    <DataGrid Grid.Column="0"
              BorderThickness="0"
              ItemsSource="{Binding ItemsTable}"
              AutoGenerateColumns="False"
              IsReadOnly="True"
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Weight" Binding="{Binding Weight}"/>
            <DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
            <DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Margin="5">
                    <TextBlock Text="{Binding Series.Name}" FontWeight="Black" FontSize="14" Margin="0,0,0,2" />
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec1.Name}" /> - <Run Text="{Binding SpecOption1.Name}" /> $<Run Text="{Binding SpecOption1.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec2.Name}" />: <Run Text="{Binding SpecOption2.Name}" /> $<Run Text="{Binding SpecOption2.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec3.Name}" />: <Run Text="{Binding SpecOption3.Name}" /> $<Run Text="{Binding SpecOption3.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec4.Name}" />: <Run Text="{Binding SpecOption4.Name}" /> $<Run Text="{Binding SpecOption4.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec5.Name}" />: <Run Text="{Binding SpecOption5.Name}" /> $<Run Text="{Binding SpecOption5.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec6.Name}" />: <Run Text="{Binding SpecOption6.Name}" /> $<Run Text="{Binding SpecOption6.Price}" /></TextBlock>
                    <TextBlock><Run FontWeight="Bold" Text="{Binding Series.Spec7.Name}" />: <Run Text="{Binding SpecOption7.Name}" /> $<Run Text="{Binding SpecOption7.Price}" /></TextBlock>
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Name}" />
                        </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="-" Margin="3,0,3,0" />
                                                <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0" />
                                                <TextBlock Text="Item(s)" />
                                            </StackPanel>
                                        </Expander.Header>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>

ItemsViewModel.cs

/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class FarrisItemsViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the FarrisItemsViewModel class.
    /// </summary>
    public FarrisItemsViewModel()
    {
        //

        ObservableCollection<Item> itemCollection = new ObservableCollection<Item>();

        itemCollection.Add(new Item("Part01", 2033, 10));
        itemCollection.Add(new Item("Part02", 4420, 10));
        itemCollection.Add(new Item("Part03", 12614, 10));

        ListCollectionView itemCollectionView = new ListCollectionView(itemCollection);

        //itemCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));

        ItemsTable = itemCollectionView;

    }

    /// <summary>
    /// The <see cref="ItemsTable" /> property's name.
    /// </summary>
    public const string ItemsTablePropertyName = "ItemsTable";

    private ListCollectionView _itemsTable = null;

    /// <summary>
    /// Sets and gets the ItemsTable property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ListCollectionView ItemsTable
    {
        get
        {
            return _itemsTable;
        }

        set
        {
            if (_itemsTable == value)
            {
                return;
            }

            RaisePropertyChanging(ItemsTablePropertyName);
            _itemsTable = value;
            RaisePropertyChanged(ItemsTablePropertyName);
        }
    }
}
4

1 回答 1

2

这是因为您没有设置Expander.Content为 display ItemsPresenter。你Expander应该看起来像这样:

<Expander>
   <Expander.Header>
     <DockPanel>
       <TextBlock Text="{Binding Path=Name}" />
       <TextBlock Text="{Binding Path=ItemCount}"/>
     </DockPanel>
   </Expander.Header>
   <Expander.Content>
     <ItemsPresenter />
   </Expander.Content>
</Expander>
于 2013-06-03T12:15:42.533 回答