【问题标题】:Can I apply a specific style to the first item of an ObservableCollection in Xamarin? [closed]我可以将特定样式应用于 Xamarin 中 ObservableCollection 的第一项吗? [关闭]
【发布时间】:2020-12-07 20:55:39
【问题描述】:

我想知道是否有一种方法可以将特定样式应用于 Xamarin 中 ObservableCollection 的第一个元素。我想对其进行自定义,以便它以比其他方式呈现的特定方式。到目前为止,我已经做了很多研究,但我找不到任何可以帮助我的东西。真的有可能做这样的事情吗?我的问题没有提供代码,因为我仍然要开始我心目中的项目。

【问题讨论】:

  • 一个 ObservableCollection 是一个数据对象,它没有任何 UI 样式。你用什么样的 UI 对象来渲染它?
  • 我考虑过使用CollectionView 并指定CollectionView.ItemTemplate
  • 你可以使用 DataTemplateSelector 但你必须在你的项目上有一些属性来区分第一个和其他

标签: c# wpf xaml xamarin observablecollection


【解决方案1】:

正如 Jason 的回复,您可以使用 DataTemplateSelector 来执行此操作,您需要一个属性来区分第一项与其他项。

我做了一个样本,你可以看看:

创建一个 DataTemplateSelector:

public class itemDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate firsttemplate { get; set; }
    public DataTemplate secondtemplate { get; set; }
    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if(((itemdata)item).isfirst==true)
        {
            return firsttemplate;
        }
        return secondtemplate;
       
    }
}

在 XAML 中使用 DataTemplateSelector

  <ContentPage.Resources>
    <DataTemplate x:Key="firstitemTemplate">
        <StackLayout Orientation="Horizontal">
            <Label
                FontSize="Large"
                Text="{Binding name}"
                TextColor="Green" />
            <Label
                FontSize="Large"
                Text="{Binding age}"
                TextColor="Green" />
        </StackLayout>
    </DataTemplate>
    <DataTemplate x:Key="seconditemTemplate">
        <StackLayout Orientation="Horizontal">
            <Label Text="{Binding name}" TextColor="Red" />
            <Label Text="{Binding age}" TextColor="Red" />
        </StackLayout>
    </DataTemplate>
    <selector:itemDataTemplateSelector
        x:Key="itemdatatemplateselector"
        firsttemplate="{StaticResource firstitemTemplate}"
        secondtemplate="{StaticResource seconditemTemplate}" />
</ContentPage.Resources>

申请Collectionview

<ContentPage.Content>
    <StackLayout>
        <CollectionView ItemTemplate="{StaticResource itemdatatemplateselector}" ItemsSource="{Binding items}" />
    </StackLayout>
</ContentPage.Content>

我使用 isfirst 布尔属性来区分第一项和其他项。

 public partial class Page14 : ContentPage
{
    public ObservableCollection<itemdata> items { get; set; }
    public Page14()
    {
        InitializeComponent();
        items = new ObservableCollection<itemdata>()
        {
            new itemdata(){name="cherry",age=28,isfirst=true },
            new itemdata(){name="cherry 1",age=28,isfirst=false },
            new itemdata(){name="cherry 2",age=28,isfirst=false },
            new itemdata(){name="cherry 3",age=28,isfirst=false },
            new itemdata(){name="cherry 4",age=28,isfirst=false }
        };
        this.BindingContext = this;
    }
}

public class itemdata
{
    public string name { get; set; }
    public int age { get; set; }

    public Boolean isfirst { get; set; }
}

【讨论】:

  • 感谢您的完整示例。不幸的是,我从外部、从 Web API 接收用作 ItemSource 的对象,它们不拥有区分第一个与第二个、第三个等的属性。所以,我需要循环它们并将isfirst 属性添加到它们中。
猜你喜欢
  • 1970-01-01
  • 2016-03-05
  • 2022-01-10
  • 2011-07-25
  • 2011-04-08
  • 2020-10-31
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多