2017-06-01 46 views
1

我在我的应用程序中有三个属性GroupName,ItemNameData。我有一个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 = vDataType = "{x:Type testProgram:MyClass}"{Binding ItemName}之间,但我不知道什么是中断或如何解决它。

回答

0

您的ControlTemplate缺少ItemsPresenter。 WPF使用GroupItem模板中的ItemsPresenter来标记扩展模板时实际项目的放置位置。由于您没有演示者,因此不会显示详细信息。

试着改变你的模板,以这样的:

<Expander IsExpanded="True"> 
    <Expander.Header> 
     <TextBlock FontWeight="Bold" Text="{Binding Name}"/> 
    </Expander.Header> 
    <Expander.Content> 
     <ItemsPresenter /> 
    </Expander.Content> 
</Expander> 
+0

就是这样。谢谢! – Kevlarz