我想让用户单击按钮后在ListView中创建新组。
private void CreateNewGroup(object sender, TappedRoutedEventArgs e)
{
//Create new group in MyListView
}
我的意思是创建类似这样的东西: Group Items in a Windows Forms ListView Control
答案 0 :(得分:0)
要对ListView
进行分组,可以使用CollectionViewSource
对项目源中的数据进行分组。
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="cvs" x:Name="cvs" />
</Grid.Resources>
<ListView Background="White" Foreground="Black" ItemsSource="{Binding Source={StaticResource cvs}}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0.5">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Complete}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<Button Content="Group" Click="Button_Click"/>
</Grid>
背后的代码
private ObservableCollection<MyClass> myClasses;
public MainPage()
{
this.InitializeComponent();
myClasses = new ObservableCollection<MyClass>();
myClasses.Add(new MyClass { Name = "Nico", Complete = false });
myClasses.Add(new MyClass { Name = "LIU", Complete = true });
myClasses.Add(new MyClass { Name = "He", Complete = true });
myClasses.Add(new MyClass { Name = "Wei", Complete = false });
myClasses.Add(new MyClass { Name = "Dong", Complete = true });
myClasses.Add(new MyClass { Name = "Ming", Complete = false });
this.cvs.Source = myClasses;
this.cvs.IsSourceGrouped = False;
}
您可以使用分组按钮方法将数据分组
private void Button_Click(object sender, RoutedEventArgs e)
{
//Group the data
//Set the grouped data to CollectionViewSource
var groups = from c in myClasses
group c by c.Complete;
this.cvs.Source = groups;
this.cvs.IsSourceGrouped = true;
}