我有DataGrid
绑定到ListCollectionView
,其中包含与ListCollectionView
的分组功能分组的几个项目。如果我不添加GroupDescription
(因此不应对任何项目进行分组),则会正确显示这些项目。但是,当我设置GroupDescription
时,我可以看到每个组的项目的名称和计数,但不显示项目本身。
有人可以指出我在这里做错了吗?
ItemsView.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);
}
}
}
答案 0 :(得分:2)
它是因为您未设置Expander.Content
来显示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>