显示值作为组标题(XAML,C#)

时间:2019-03-29 09:20:58

标签: c# wpf xaml mvvm grouping

我有ListView

<ListView ItemsSource="{Binding Path=ListUsers}" DockPanel.Dock="Bottom" >
 <ListView.View>
  <GridView>
   <GridViewColumn Header="Position" DisplayMemberBinding="{Binding Position}" />
   <GridViewColumn Header="Counter"  DisplayMemberBinding="{Binding Counter}" /> 
 </GridView>
</ListView.View>

我做了ListView.GroupStyle

<ListView.GroupStyle>
 <GroupStyle>
  <GroupStyle.ContainerStyle>
   <Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
     <Setter.Value>
      <ControlTemplate>
       <Expander IsExpanded="True">
        <Expander.Header>
         <StackPanel Orientation="Horizontal">
           <TextBlock Text="ReadTime" FontSize="14" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
          <TextBlock Text="{Binding ReadTime}" VerticalAlignment="Bottom" />
          </StackPanel>
         </Expander.Header>
        <ItemsPresenter />
       </Expander>
      </ControlTemplate>
     </Setter.Value>
    </Setter>
   </Style>
  </GroupStyle.ContainerStyle>
 </GroupStyle>
</ListView.GroupStyle>

ctor的{​​{1}}中,我确实是这样的:

ViewModel

现在如何代替ReadTime中的每个组CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(ListUsers); PropertyGroupDescription groupDescription = new PropertyGroupDescription("ReadTime"); view.GroupDescriptions.Add(groupDescription); ,而不是每个组的display text "ReadTime"

1 个答案:

答案 0 :(得分:1)

  

现在如何显示来自ReadTime的值,而不是为每个组显示文本“ ReadTime”?

没有 the 值,因为每个组都有一个标头,并且一个组可能包含多个值。如果要在组中显示 first 值,则可以使用组的Items集合:

<TextBlock Text="{Binding Items[0].ReadTime}" VerticalAlignment="Bottom" />