1
我在我的应用程序中有三个属性GroupName
,ItemName
和Data
。我有一个ListView
将这些类的集合按GroupName
分组,并在文本框中显示它们的ItemName
属性。问题是,当我运行代码时,这些组显示正确,但没有一个显示任何成员。为什么WPF ListView中的组为空?
这里是XAML代码:
<ListView x:Name="MyList">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock FontWeight="Bold" Text="{Binding Name}"/>
</Expander.Header>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type testProgram:MyClass}">
<TextBlock Text="{Binding ItemName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这里是后面的代码:
public partial class MyListView
{
public MyListView(ObservableCollection<MyClass> items)
{
InitializeComponent();
Items = items;
var v = CollectionViewSource.GetDefaultView(Items);
v.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));
MyList.ItemsSource = v;
}
public ObservableCollection<MyClass> Items { get; set; }
}
当我删除了<ListView.GroupStyle>...
并设置MyList.ItemsSource = Items;
然后一切正常显示了。 我怀疑问题是在ItemsSource = v
,DataType = "{x:Type testProgram:MyClass}"
和{Binding ItemName}
之间,但我不知道什么是中断或如何解决它。
就是这样。谢谢! – Kevlarz