您好我正在尝试数据绑定位于LLS的ItemTemplate内的ListBoxes,到目前为止,我已经看到分组应用于仅包含Textblocks的模板,但我需要将'collection'分组。我知道我的要求不是那么多实用但我非常需要它。
我的模特
public class ItemViewModel
{
private string _id;
public string ID
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _lineOne;
public string LineOne
{
get
{
return _lineOne;
}
set
{
if (value != _lineOne)
{
_lineOne = value;
NotifyPropertyChanged("LineOne");
}
}
}
private string _lineTwo;
public string LineTwo
{
get
{
return _lineTwo;
}
set
{
if (value != _lineTwo)
{
_lineTwo = value;
NotifyPropertyChanged("LineTwo");
}
}
}
}
MY VIEWMODELS
public class GroupedItemViewModel
{
public string Key2
{
get
{
return _key2;
}
set
{
if (value != _key2)
{
_key2 = value;
NotifyPropertyChanged("Key2");
}
}
}
private ObservableCollection<ItemViewModel> _grp;
public ObservableCollection<ItemViewModel> GroupedItems
{
get
{
return _grp;
}
set
{
if (value != _grp)
{
_grp= value;
NotifyPropertyChanged("GroupedItems");
}
}
}
}
public class MainViewModel : ViewModelBase,INotifyPropertyChanged
{
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
public ObservableCollection<ItemViewModel> Items { get; private set; }
public ObservableCollection<GroupedItemViewModel> GroupedPhotos
{
get
{
var finalQuery = Items
.GroupBy(category => category.LineOne)
.Select(grouping => new GroupedItemViewModel { Key2 = grouping.Key, GroupedItems = grouping.ToObservableCollection<ItemViewModel>() });
return new ObservableCollection<GroupedItemViewModel>(finalQuery);
}
}
}
我的观点 - LLS
<Grid x:Name="ContentPanel">
<phone:LongListSelector ItemsSource="{Binding GroupedPhotos}" ItemTemplate="{StaticResource DataTemplate3}" GroupHeaderTemplate="{StaticResource header}"/>
</Grid>
我的观点 - LLS的数据模板
<DataTemplate x:Key="DataTemplate3">
<Grid>
<ListBox ItemsSource="{Binding GroupedItems}" ItemTemplate="{StaticResource DataTemplate4}" ItemsPanel="{StaticResource ItemsPanelTemplate2}"/>
</Grid>
</DataTemplate>
我的观点 - 列表的数据模板
<DataTemplate x:Key="DataTemplate4">
<Grid>
<TextBlock Text="{Binding LineOne}"/>
</Grid>
</DataTemplate>
我的观点 - 组头的数据模板
<DataTemplate x:Key="header">
<Grid>
<TextBlock Text="{Binding Key2}"/>
</Grid>
</DataTemplate>