1
我有一個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);
}
}
}
現在,我看到你的解決方案,我有點記住,但我找不到一些搜索後找到它。非常感謝您指出這一點,現在完美地工作! – Kryptoxx