C# Expander标头显示具有特定属性的项目数
我有一个listview,它按底层数据源的属性分组。 Groupstyle包含一个带有标题的扩展器,我想在其中显示各种内容。 列表视图显示电子邮件,例如按主题分组。 我知道要在每个组标题中显示(未读邮件数/项目) 到目前为止,我的解决方案是:C# Expander标头显示具有特定属性的项目数,c#,wpf,expander,C#,Wpf,Expander,我有一个listview,它按底层数据源的属性分组。 Groupstyle包含一个带有标题的扩展器,我想在其中显示各种内容。 列表视图显示电子邮件,例如按主题分组。 我知道要在每个组标题中显示(未读邮件数/项目) 到目前为止,我的解决方案是: <ListView Name="Mails" local:FM.Register="{Binding}" local:FM.GetFocus="Loaded" Grid.Row="1" HorizontalAlig
<ListView Name="Mails" local:FM.Register="{Binding}" local:FM.GetFocus="Loaded"
Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Path=MailsProxy.View}"
SelectionMode="Single" SelectedItem="{Binding Path=SelectedMail, Mode=TwoWay}"
local:SortList.BringIntoViewSelected="True" local:SortList.IsGridSortable="True"
ItemContainerStyle="{StaticResource InboxMailItem}"
View="{Binding Source={x:Static session:Session.Current}, Path=InboxView.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 Foreground="Black" BorderThickness="0,0,0,1" Style="{StaticResource ExpanderStyle}" Expanded="OnExpand" Collapsed="OnCollapse" Loaded="OnLoad">
<Expander.Header>
<DockPanel>
<TextBlock FontSize="14" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.GroupBy}"/>
<TextBlock FontSize="14">:</TextBlock>
<TextBlock FontSize="14" Text="{Binding Path=Name, Converter={StaticResource GroupHeaderConverter}}" Margin="5,0,0,0"/>
<TextBlock> </TextBlock>
<TextBlock FontSize="14" Margin="0,1,0,0">(</TextBlock>
<TextBlock FontSize="14" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.Unread}"/>
<!--<TextBlock FontSize="14" Text="{Binding StringFormat=0, Converter={StaticResource InboxGroupSeenConverter}}" />-->
<TextBlock FontSize="14">/</TextBlock>
<TextBlock FontSize="14" Text="{Binding Path=ItemCount}"/>
<TextBlock FontSize="14">)</TextBlock>
</DockPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="DataContext" Value="{Binding Source={x:Static session:Session.Current}, Path=InboxView}"/>
</Style>
</ListView.Resources>
</ListView>
有没有一种方法可以通过转换器实现这一点,或者我应该使用上面所示的Properteis来实现另一种方法()
编辑:澄清我的问题。“我的组”显示的标题必须显示两个不同的数字。一个是组的ItemCount,第二个是此组中未读项目的数量。
未读项目的数量是在我的转换器中计算的,这对我来说是一个测试,但是如果基础集合属性发生更改,此解决方案不会更新视图。我认为您的
扩展器需要DynamicSource样式。
顺便说一句,扩展器有自己的特性,您可以将其用于解决方案。这是我之前回答的问题中的一个示例
祝你好运,让我们知道:-)
编辑
明天我会回到你身边,因为我即将完成工作:-),记住expander有自己的itemsCount,所以不需要实现自己的,第二件事是在你的email对象属性bool Unread
中实现,而不仅仅是使用DataTriggers和voila将它绑定到你的textblock样式 你能再精确一点吗?请告诉我如何使用这种样式设置标题/绑定文本属性?好吧,与其在样式中使用StaticResource,不如使用下面的内容
这种样式不是我的问题。我希望Expanders Dockpanel中TextBlock元素的Text属性具有适当的绑定,这可以解决我的更新问题,如果使用转换器,我会得到更新。可能是我的英语或其他内容,但您不了解我的问题。我知道扩展器有自己的属性itemcount,我已经在使用它了。此外,我的邮件项还有一个属性read/unread,我正在转换器中使用该属性。问题是获取每个扩展器组中所有未读项目的数量,并显示该数量。扩展器用作邮件项的分组机制。邮件项目可以按主题分组。因此,一个组可以包含10个项目(ItemCount),但其中只有5个项目已经标记为read(Mailitem.read)。现在我想在这个组标题中显示5/10。嗨,朋友,我想你的英语点对了,我是错的:-)总之,回到问题上来,你是否尝试过在
public class InboxGroupSeenConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return GetTotalUnread(value as CollectionViewGroup);
}
private static int GetTotalUnread(CollectionViewGroup group)
{
int count = 0;
foreach (eMail mailItem in group.Items)
if (mailItem.Seen == false)
count++;
return count;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}