Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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# CollectinView分组不显示最后一个组中的所有项目_C#_Android_Xamarin_Xamarin.forms_Collectionview - Fatal编程技术网

C# CollectinView分组不显示最后一个组中的所有项目

C# CollectinView分组不显示最后一个组中的所有项目,c#,android,xamarin,xamarin.forms,collectionview,C#,Android,Xamarin,Xamarin.forms,Collectionview,我正在用Xamarin的形式为Android开发一个移动应用程序,但有一个问题。我有一个可观察的集合,我用下面的模型填充它 private int _category_ID; public int Category_ID { get { return _category_ID; } set { _category_ID = value;

我正在用Xamarin的形式为Android开发一个移动应用程序,但有一个问题。我有一个可观察的集合,我用下面的模型填充它

    private int _category_ID;
    public int Category_ID
    {
        get
        {
            return _category_ID;
        }
        set
        {
            _category_ID = value;
            OnPropertyChanged("_category_ID");
        }
    }
    private string _category_Name;
    public string Category_Name
    {
        get
        {
            return _category_Name;
        }
        set
        {
            _category_Name = value;
            OnPropertyChanged("_category_Name");
        }
    }
    private string _category_Description;
    public string Category_Description
    {
        get
        {
            return _category_Description;
        }
        set
        {
            _category_Description = value;
            OnPropertyChanged("_category_Description");
        }
    }
    public CategoryModel(string name, List<ProductModel> products) : base(products)
    {
        Category_Name = name;
    }
private int\u category\u ID;
公共int类别\u ID
{
得到
{
返回(类别)ID;
}
设置
{
_类别ID=值;
OnPropertyChanged(“类别ID”);
}
}
私有字符串_类别_名称;
公共字符串类别名称
{
得到
{
返回(类别)名称;;
}
设置
{
_类别名称=值;
不动产变更(“类别名称”);
}
}
私有字符串_类别_描述;
公共字符串类别描述
{
得到
{
返回(类别)描述;;
}
设置
{
_类别描述=价值;
不动产变更(“类别描述”);
}
}
公共CategoryModel(字符串名称,列出产品):基本(产品)
{
类别名称=名称;
}
这很好,当我在ObservableCollection中调试时,所有类别和项都显示正确。示例:类别1=2项类别2=3项类别3=4项。 这很有效

但我的问题是,当我使用像这样的CollectionView时

      <CollectionView ItemsSource="{Binding ObservCollectionCategory}" IsGrouped="True">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <ScrollView>
                            <Grid Padding="10">
                                <StackLayout BackgroundColor="Blue">
                                    <Label Text="{Binding Product_Name}"/>
                                    <Label Text="{Binding Product_Description}"/>
                                </StackLayout>
                            </Grid>
                            </ScrollView>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                    <CollectionView.GroupHeaderTemplate>
                        <DataTemplate >
                            <Label Text="{Binding Category_Name}"/>
                        </DataTemplate>
                    </CollectionView.GroupHeaderTemplate>
                </CollectionView>

该视图仅显示最后一个类别的第一项,另一项忽略。所有其他类别都显示正确。 当我在ObservableCollection末尾创建一个空白类别时,最后一个类别中的所有项目都会显示在视图中。但最后我有一个空组


我已尝试在页眉中显示计数,而计数是正确的(4)。

您可以尝试下面的代码

型号:

 public class ProductModel : INotifyPropertyChanged
{

    private string _product_Name;
    public string Product_Name
    {
        get
        {
            return _product_Name;
        }
        set
        {
            _product_Name = value;
            OnPropertyChanged("Product_Name");
        }
    }
    private string _product_Description;
    public string Product_Description
    {
        get
        {
            return _product_Description;
        }
        set
        {
            _product_Description = value;
            OnPropertyChanged("Product_Description");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}
public class CategoryModel : List<ProductModel>, INotifyPropertyChanged
{
    private string _category_Name;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Category_Name
    {
        get
        {
            return _category_Name;
        }
        set
        {
            _category_Name = value;
            OnPropertyChanged("Category_Name");
        }
    }

    public CategoryModel(string name, List<ProductModel> diary) : base(diary)
    {
        Category_Name = name;
    }
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共类ProductModel:INotifyPropertyChanged
{
私有字符串_产品_名称;
公共字符串产品名称
{
得到
{
返回产品名称;
}
设置
{
_产品名称=价值;
OnProperty变更(“产品名称”);
}
}
私有字符串_产品_描述;
公共字符串产品描述
{
得到
{
退货-产品描述;
}
设置
{
_产品描述=价值;
OnProperty变更(“产品描述”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共类CategoryModel:列表,INotifyPropertyChanged
{
私有字符串_类别_名称;
公共事件属性更改事件处理程序属性更改;
公共字符串类别名称
{
得到
{
返回(类别)名称;;
}
设置
{
_类别名称=值;
OnProperty变更(“类别名称”);
}
}
公共CategoryModel(字符串名称,列表日志):基本(日志)
{
类别名称=名称;
}
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
代码隐藏:

public ObservableCollection<CategoryModel> ObservCollectionCategory { get; set; } = new ObservableCollection<CategoryModel>();

    public Page18()
    {
        InitializeComponent();
        ObservCollectionCategory.Add(new CategoryModel("Categroy 1", new List<ProductModel>
        {
            new ProductModel(){ Product_Name="item1", Product_Description="Description1"},
            new ProductModel(){ Product_Name="item2", Product_Description="Description2"},
        }));
        ObservCollectionCategory.Add(new CategoryModel("Categroy 2", new List<ProductModel>
        {
            new ProductModel(){ Product_Name="item1", Product_Description="Description1"},
            new ProductModel(){ Product_Name="item2", Product_Description="Description2"},
            new ProductModel(){ Product_Name="item3", Product_Description="Description3"},
        }));
        ObservCollectionCategory.Add(new CategoryModel("Categroy 3", new List<ProductModel>
        {
            new ProductModel(){ Product_Name="item1", Product_Description="Description1"},
            new ProductModel(){ Product_Name="item2", Product_Description="Description2"},
            new ProductModel(){ Product_Name="item3", Product_Description="Description3"},
            new ProductModel(){ Product_Name="item4", Product_Description="Description4"},
        }));

        this.BindingContext = this;
    }
public observeCollection observeCollectionCategory{get;set;}=new observeCollection();
公共页18()
{
初始化组件();
ObserveCollectionCategory.Add(新类别模型(“类别1”,新列表
{
new ProductModel(){Product_Name=“item1”,Product_Description=“Description1”},
new ProductModel(){Product_Name=“item2”,Product_Description=“Description2”},
}));
添加(新类别模型(“类别2”,新列表
{
new ProductModel(){Product_Name=“item1”,Product_Description=“Description1”},
new ProductModel(){Product_Name=“item2”,Product_Description=“Description2”},
新产品模型(){Product\u Name=“item3”,Product\u Description=“Description3”},
}));
ObserveCollectionCategory.Add(新类别模型(“类别3”,新列表
{
new ProductModel(){Product_Name=“item1”,Product_Description=“Description1”},
new ProductModel(){Product_Name=“item2”,Product_Description=“Description2”},
新产品模型(){Product\u Name=“item3”,Product\u Description=“Description3”},
new ProductModel(){Product_Name=“item4”,Product_Description=“Description4”},
}));
this.BindingContext=this;
}
输出:


1)显示创建项目和组的代码,并将组添加到ObserveCollectionCategory。2) 是否将组添加到ObserveCollectionCategory,然后将项目添加到该组?改变方法:在将组添加到集合之前,将项目添加到组。3) 您的
OnPropertyChanged
调用错误。看看是否可以将它们更改为simply
OnPropertyChanged()-无参数。如果没有编译,那么给出属性名,而不是私有字段名:
OnPropertyChanged(“Category_ID”)@ToolmakerSteve谢谢你,它能工作。你是对的。我首先将集合添加到ObserveCollection,然后再添加项。现在我创建了一个圣殿骑士,在这里我用物品和foreach填充类别