对于以下情况,我无法使数据绑定正常工作:
模型 - DetailEntries
的集合,以及DailyDetailSet
的一些集体属性:
[AddINotifyPropertyChangedInterface]
public class DailyDetailsSet
{
public DailyDetailsSet(DateTime startDate, ObservableCollection<DailyDetailEntry> entries)
{
this.StartDate = startDate;
this.Entries = entries;
}
public DateTime StartDate { get; }
public decimal TotalHours => this.Entries.Sum(x => x.TotalHours);
public string TotalHoursFormatted => this.TotalHours.ToFormattedDurationString();
public ObservableCollection<DailyDetailEntry> Entries { get; }
public DetailsGroupingHeader DetailsGroupingHeader => new DetailsGroupingHeader(this.StartDate, this.TotalHours);
}
[AddINotifyPropertyChangedInterface]
public class DailyDetailEntry
{
//some properties
}
public class DetailsGroupingHeader
{
//Some text and color properties and overriden Equals() method to allow grouping
}
ViewModel - 从数据库中检索DailyDetailsSet
的集合:
[AddINotifyPropertyChangedInterface]
public class EntryListViewModel : ViewModelBase
{
public ObservableCollection<DailyDetailsSet> EntrySets { get; set; }
}
}
一个视图应该包含一个列表框,它应该包含所有DailyDetailEntries
,根据DetailsGroupingHeader(它真正比较输入日期)分组到每一天的组中。
查看:
<UserControl.Resources>
<CollectionViewSource x:Key="Sets" Source="{Binding Path=EntrySets}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DetailsGroupingHeader" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<ListView ItemsSource="{Binding}" DataContext="{StaticResource Sets}" DockPanel.Dock="Bottom" Margin="5">
<ListView.View >
<GridView >
<GridViewColumn DisplayMemberBinding="{Binding StartTimeFormatted}" Header="Start" Width="60" />
<GridViewColumn DisplayMemberBinding="{Binding Comment}" Header="Comment" >
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False" BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock Foreground="{Binding Path=Name.EntryColor}" FontWeight="Bold" Text="{Binding Path=Name.TotalHoursFormatted}"/>
<TextBlock Foreground="{Binding Path=Name.EntryColor}" FontWeight="Bold" Text=" hours - "/>
<TextBlock Foreground="{Binding Path=Name.EntryColor}" FontWeight="Bold" Text="{Binding Path=Name.EntryDateFormatted}"/>
<TextBlock Foreground="{Binding Path=Name.EntryColor}" FontWeight="Bold" Text=" Entries: "/>
<TextBlock Foreground="{Binding Path=Name.EntryColor}" FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
现在,它不能完全正常工作 - 我无法让CollectionViewSource
基于内部集合工作(每个EntrySet包含应该是实际数据源的Entries集合 - EntrySet用于分组和自定义组头显示)。
当前代码给出以下结果:
因此,这表明绑定'排序'有效,但组内不包含实际条目(只有DailyDetailEntry
和DailyDetailSet
中相同的单个属性。)
如何使listview使用集合的集合属性作为项目源?
我在绑定中尝试了很多不同的ItemsSource声明,包括指定Path或类似<CollectionViewSource x:Key="Sets" Source="{Binding Path=EntrySets/Entries}">
的语法,但在大多数情况下结果更糟。