繁体   English   中英

从列表填充分组的ListView

[英]Populating a grouped ListView from a list

我有一大堆通过解析JSON文件检索的对象。 现在,我将所述列表绑定到ListView但是该列表很笨拙,我想将其分成单独的组以方便使用。 我尝试遵循几种不同的指南,但无法以正确的方式准备数据。 如果我用某些项目手动初始化一个排序列表,它们确实会显示出来,那么代码确实起作用了。

我的分组模型:

public class SortedItem
{
    public string Header { get; set; }
    public List<Item> Items { get; set; }

    public SortedItem(string header)
    {
        Header = header;
    }
}

我的对象模型:

public class Item
{
    public string item { get; set; }
    //public int icon { get; set; }

    private string ico;
    public string icon
    {
        get { return ico; }
        set { ico = "Icons/" + value + ".png"; }
    }

    public int id { get; set; }
    public string slot { get; set; }
    public string scrip { get; set; }
    public Reduce reduce { get; set; }
    public int lvl { get; set; }
    public string zone { get; set; }
    public int time { get; set; }
}

现在,我的XAML如下:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Class="Eorzea_Gatherer.Pages.NodesPage"
     xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
     ios:Page.UseSafeArea="true"
     BackgroundColor="#F4F4F4">
<!--https://xamarinhelp.com/safeareainsets-xamarin-forms-ios/-->
<ListView x:Name="nodesListView"
      IsGroupingEnabled="True"
      GroupDisplayBinding="{Binding Header}"
      HasUnevenRows="True"
      BackgroundColor="#F4F4F4"
      Margin="30, 30, 30, 0">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid Padding="0, 5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding icon}"
                       HeightRequest="50"
                       WidthRequest="50"
                       Grid.Column="0"/>
                <StackLayout Grid.Column="1">
                    <Label Text="{Binding item}"
                           TextColor="#171717"
                           FontSize="13"
                           FontFamily="SegoeUI"/>
                    <!--https://forums.xamarin.com/discussion/97996/binding-more-than-one-property-in-listview-->
                    <Label TextColor="#171717"
                           FontSize="12"
                           FontFamily="SegoeUI">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="{Binding zone}"/>
                                <Span Text=" - "/>
                                <Span Text="{Binding slot}"/>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                    <Label TextColor="#171717"
                           FontSize="12"
                           FontFamily="SegoeUI">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="{Binding time}"/>
                                <Span Text=" - "/>
                                <Span Text="00:00 AM"/>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                </StackLayout>
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>
</ListView>

我用来检索列表并将其作为源绑定到ListView的函数:

public static List<SortedItem> GetSortedItems()
{
    List<Item> items = GetItems();

    List<SortedItem> sortedItems = new List<SortedItem>()
    {
        new SortedItem("50")
        {
           Items = items.Where(x => x.lvl == 50).ToList()
        },
        new SortedItem("55"),
        new SortedItem("60"),
        new SortedItem("65"),
        new SortedItem("70")
    };

    return sortedItems;
}

使用我的代码,我可以在ListView中看到不同的组(50、55,...),但没有其他弹出窗口。 我确定我的问题是获取对象列表并以适当的方式将其拆分,但是我很困惑。 使我感到困惑的是,在调试过程中,将sortedItems悬停在生成的sortedItems我看到我的第一组确实包含了所需的对象,但它们仍未显示在视图中。

试试这个,从James Montemagno偷来的

public class Grouping<K, T> : ObservableCollection<T>
{
  public K Key { get; private set; }

  public Grouping(K key, IEnumerable<T> items)
  {
      Key = key;
      foreach (var item in items)
        this.Items.Add(item);
  }
}

var sorted = from item in Items
             orderby item.lvl
             group item by item.lvl into itemGroup
             select new Grouping<int, Item>(itemGroup.Key, itemGroup);

//create a new collection of groups
ItemsGrouped = new ObservableCollection<Grouping<int, Item>>(sorted);

然后在您的XAML中

GroupDisplayBinding="{Binding Key}"

您应该使分组模型继承自ObservableCollection

public class SortedItem : ObservableCollection<Item>
{
    public string Header { get; set; }

    public SortedItem(List<Item> list) : base(list)
    {

    }
}

然后像这样排序:

public static List<SortedItem> GetSortedItems()
{
    List<Item> items = GetItems();

    List<SortedItem> sortedItems = new List<SortedItem>()
    {
        new SortedItem(items.Where(x => x.lvl == 50).ToList())
        {
            Header = "50"
        },
        new SortedItem(items.Where(x => x.lvl == 55).ToList())
        {
            Header = "55"
        },
        new SortedItem(items.Where(x => x.lvl == 60).ToList())
        {
            Header = "60"
        },
        new SortedItem(items.Where(x => x.lvl == 65).ToList())
        {
            Header = "65"
        },
        new SortedItem(items.Where(x => x.lvl == 70).ToList())
        {
            Header = "70"
        }
    };

    return sortedItems;
}

此外,尝试在模型中实现INotifyPropertyChanged接口。 否则,如果您在运行时更改了模型的属性,则不会通知UI。

我可能错过了一些东西,但是您将List<item> Items绑定在哪里? 我认为您的ListView缺少诸如ItemSource="{Binding Items}"东西,看来您的ViewCell绑定良好,应该可以按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM