我已经使用ListView实现了Expander,但是尝试绑定到ViewModel的属性时,没有填充Header Text。 ReportCategoryTypeName是我要绑定到Expander标头的内容,但是所有标头都是空白。
如果我对文本值进行硬编码,则一切正常。
我的代码基于:https://www.wpf-tutorial.com/listview-control/listview-grouping/,我知道它一直在使用代码。
这是背后的代码:ucAppReports.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace App.Pages
{
public partial class ucAppReports : UserControl
{
public ucAppReports()
{
InitializeComponent();
CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lvReports.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("ReportCategoryTypeName");
cv.GroupDescriptions.Add(groupDescription);
}
}
}
这是ucAppReports.xaml中的ListView
<ListView x:Name="lvReports" HorizontalAlignment="Stretch"
ItemsSource="{Binding Reports}"
SelectedItem="{Binding SelectedReport}" Grid.Row="1"
Grid.Column="0" Background="Green" BorderBrush="Purple" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true" >
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.groupstyle>
<groupstyle>
<groupstyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ReportCategoryTypeName}" FontWeight="Bold" Foreground="Black" FontSize="22" VerticalAlignment="Bottom" />
<!--<TextBlock Text="Hello" FontWeight="Bold" Foreground="Black" FontSize="22" VerticalAlignment="Bottom" />-->
<!--<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />-->
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</groupstyle.ContainerStyle>
</groupstyle>
</ListView.groupstyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Orange" BorderThickness="0,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<!--<RowDefinition Height="22"></RowDefinition>-->
</Grid.RowDefinitions>
<Label Content="{Binding ReportName}" FontSize="14" Grid.ColumnSpan="2" Grid.Row="0" ></Label>
<!--<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Content="Modififed Date:" HorizontalAlignment="Right" FontSize="10" Width="79"></Label>
<Label Content="{Binding ModifiedDate}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="10" Width="auto" Height="22"></Label>
</StackPanel>-->
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ListView项目源是Reports,并且已在AppReportsViewModel上正确填充了它。
如果取消注释此行,则所有报告都将正确分组。唯一的问题是所有标头都是Hello。
<!--<TextBlock Text="Hello" FontWeight="Bold" Foreground="Black" FontSize="22" VerticalAlignment="Bottom" />-->