Your example has a few typos and is incomplete so it is difficult to diagnose your problem directly.
Here is an example of putting a CollectionViewSource with Grouping in the ViewModel and setting the context of the container in the code behind.
(Of course doing it this way, you can't see the result at design time--only when the program is run.)
XAML
<Page
x:Class="CVSinVM.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CVSinVM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="container">
<GridView x:Name="items" ItemsSource="{Binding}">
<GridView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="20,0,0,0"/>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding BandName}" FontSize="14.667" Foreground="Red"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
</Grid>
</Grid>
</Page>
C#
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace CVSinVM
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var vm = new ViewModel();
container.DataContext = vm.myNewCVS;
}
}
public class Band
{
public string BandName { get; set; }
public List<Member> Members { get; set; }
public Band()
{
Members = new List<Member>();
}
}
public class Member
{
public string Name { get; set; }
}
public class ViewModel
{
public CollectionViewSource myNewCVS { get; set; }
public List<Band> Bands { get; set; }
public ViewModel()
{
Bands = new List<Band>();
Band band1 = new Band();
band1.BandName = "The Beatles";
band1.Members.Add(new Member { Name = "John" });
band1.Members.Add(new Member { Name = "Paul" });
band1.Members.Add(new Member { Name = "George" });
band1.Members.Add(new Member { Name = "Ringo" });
Bands.Add(band1);
Band band2 = new Band();
band2.BandName = "The Beatles";
band2.Members.Add(new Member { Name = "Mick" });
band2.Members.Add(new Member { Name = "Keith" });
band2.Members.Add(new Member { Name = "Charlie" });
band2.Members.Add(new Member { Name = "Ronnie" });
Bands.Add(band2);
myNewCVS = new CollectionViewSource();
myNewCVS.Source = Bands;
myNewCVS.IsSourceGrouped = true;
myNewCVS.ItemsPath = new Windows.UI.Xaml.PropertyPath("Members");
}
}
}
No comments:
Post a Comment