首页 > 解决方案 > 在wpf中对listview中的项目进行分组时如何在扩展器标题中绑定类属性

问题描述

我正在对列表视图中的项目进行分组,如下所示。现在我想在扩展头(组头)中绑定我的一些类属性,我无法在该<ListView.GroupStyle>部分中绑定我的类中的任何属性,但能够在<ListView.View>.

编写我尝试过的代码:

<UserControl x:Class="ClientAccessTool.CasesAndTasksDetailsControl"
xmlns:core="clr-namespace:ClientAccessTool.Core;assembly=ClientAccessTool.Core"
DataContext="{x:Static core:SiteListViewModel.Instance}"
>
<UserControl.Resources>
    <CollectionViewSource x:Key="groupByCases2" Source="{Binding SelectedSite.CasesAndTasksList}" >
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="CaseName" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>


<Grid>
 <ListView x:Name="lvCasesAndTasks" 
               ItemsSource="{Binding Source={StaticResource groupByCases2}}">
<ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Border  Margin="3" Width="383" HorizontalAlignment="Left"  >
                                        <Expander x:Name="lvExpander"  IsExpanded="True"
                                              Style="{StaticResource ExpanderStyle3}" >
                                        <Expander.Header>
                                               <Grid VerticalAlignment="Center" >
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="Auto" />
                                                 </Grid.ColumnDefinitions>
                                                    <Viewbox Height="23" Margin="5" Grid.Column="0" DockPanel.Dock="Left">
                                                        <Image  Source="/Images/RedFlag.png" RenderOptions.BitmapScalingMode="HighQuality"/>
                                                    </Viewbox>
                                                    <Grid Grid.Column="1" MinWidth="250" >
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition />
                                                            <RowDefinition />
                                                        </Grid.RowDefinitions>
                                                    <TextBlock Grid.Row="0"
                                                        Text="{Binding Name}" 
                                                           FontSize="18" VerticalAlignment="Center"
                                                           FontWeight="SemiBold"
                                                            Foreground="Red"
                                                               Margin="5"                                                                   
                                                            />
                                                        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5" >
                                                            <TextBlock Text="Number of tasks:"  FontFamily="{StaticResource FontAwesome}" Foreground="Gray"/>
                                                            <TextBlock Margin="5 0 0 0"   
                                                                       Text="{Binding CountOfTasks}"
                                                                       FontFamily="{StaticResource FontAwesome}" Foreground="Gray" >
                                                            </TextBlock>
                                                        </StackPanel>
                                                    </Grid>
                                                </Grid>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>                  
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

c#类:

namespace ClientAccessTool.Core
{
    public class Case 
    {
        public string SiteName { get; set; }

        public string CaseName { get; set; }

        public string CaseDescription { get; set; }

        public List<Tasks> ListOfTasks { get; set; } = new List<Tasks>();

        public string CountOfTasks => ListOfTasks.Count.ToString() ;
    }
}   

项目源 SelectedSite.CasesAndTasksList 是 Case 类型的 List。我想在扩展器标头中绑定 CountOfTasks 属性,但它不起作用。

标签: c#wpf

解决方案


推荐阅读