Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ItemTemplate中对包含列表框的LongListSelector进行分组_C#_Xaml_Windows Phone - Fatal编程技术网

C# 在ItemTemplate中对包含列表框的LongListSelector进行分组

C# 在ItemTemplate中对包含列表框的LongListSelector进行分组,c#,xaml,windows-phone,C#,Xaml,Windows Phone,您好,我正在尝试将驻留在LLS的ItemTemplate中的列表框数据绑定,到目前为止,我已经看到分组应用于只包含文本块的模板,但我需要将“集合”按分组。我知道我的要求不太实际,但我非常需要它 我的模型 public class ItemViewModel { private string _id; public string ID { get { return _id; } set

您好,我正在尝试将驻留在LLS的ItemTemplate中的列表框数据绑定,到目前为止,我已经看到分组应用于只包含文本块的模板,但我需要将“集合”按分组。我知道我的要求不太实际,但我非常需要它

我的模型

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");
            }
        }
    }

}
我的视图模型

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);

        }
    }
public类GroupedItemViewModel
{
公共字符串键2
{
得到
{
返回键2;
}
设置
{
如果(值!=\u键2)
{
_键2=值;
NotifyPropertyChanged(“Key2”);
}
}
}
私人可观测收集(grp);;
公共可观测集合GroupedItems
{
得到
{
返回grp;
}
设置
{
如果(值!=\u grp)
{
_grp=价值;
NotifyPropertyChanged(“GroupedItems”);
}
}
}
}
公共类MainViewModel:ViewModelBase,INotifyPropertyChanged
{
公共主视图模型()
{
this.Items=新的ObservableCollection();
}
公共可观测集合项{get;private set;}
公共可观察收集组照片
{
得到
{
var finalQuery=项目
.GroupBy(category=>category.LineOne)
.Select(grouping=>newgroupeditemviewmodel{Key2=grouping.Key,GroupedItems=grouping.ToObservableCollection()});
返回新的ObservableCollection(finalQuery);
}
}
}

我的看法

<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>