9阅网

您现在的位置是:首页 > 知识 > 正文

知识

listview - 删除一行后更新ListView数据模板。

admin2022-11-07知识18

我在使用ListView时遇到了一个意想不到的情况。我有一个ListView,它的行根据其奇偶性有一个背景色。它工作得很好,直到我从绑定的集合中移除一个元素,然后两行具有相同的背景颜色的行一起出现。这是我的代码。

<ResourceDictionary>
  <DataTemplate x:Key="EvenIndexDataTemplate">
      <ViewCell>
          <Grid BackgroundColor="LightGray"">
                <!-- my content here -->
          </Grid>
      </ViewCell>
  </DataTemplate>

  <DataTemplate x:Key="OddIndexDataTemplate">
      <ViewCell>
          <Grid BackgroundColor="White"">
                <!-- my content here -->
          </Grid>
      </ViewCell>
  </DataTemplate>

  <datatemp:EvenOddTemplateSelector x:Key="MyDataTemplate"
                                    EvenTemplate="{StaticResource EvenIndexDataTemplate}"
                                    OddTemplate="{StaticResource OddIndexDataTemplate}" />
</ResourceDictionary>



<ContentPage.Content>

    <!-- something here -->

    <ListView ItemsSource="{Binding ItemsSource}" 
              ItemTemplate="{StaticResource ItemTemplateResource}" />

    <!-- something else here -->

</ContentPage.Content>

我的DataTemplateSelector类:

public class EvenOddTemplateSelector : DataTemplateSelector
{
    public DataTemplate EvenTemplate { get; set; }

    public DataTemplate OddTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        var itemsView = (ListView)container;
        return ((IList)itemsView.ItemsSource).IndexOf(item) % 2 == 0 ? EvenTemplate : OddTemplate;
    }

我想得到的是在删除一个元素后,保留相邻的两行不同的背景色。在这一点上,ItemsSource只被AddingRemoving修改。我还没有尝试插入一个项目。我不需要它来实现我正在做的这个功能。

有什么好办法吗?

快速演示



【回答】:

我刚刚解决了这个问题。我是这样做的。

我创建了一个自定义的 ViewCell 类,并添加了三个可绑定的属性 ContainerItemsSource, EvenBackgroundColorOddBackgroundColor然后,我覆盖 OnBindingContextChanged() 以确保设置 View 背景色由其在集合内的索引。

你可以在这里查看 新辫子 在github repo上。

基本上的想法是用这种方式。

<ListView ItemsSource="{Binding YOUR_ITEMS_SOURCE}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:EvenOddCell ContainerItemsSource="{Binding YOUR_ITEMS_SOURCE, 
                                     Source={x:Reference YOUR_PAGE_BINDING_CONTEXT}}" 
                               EvenBackgroundColor="AliceBlue"
                               OddBackgroundColor="White">

                <!-- YOUR CELL CONTENT -->

            </local:EvenOddCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我自定义的 EvenOddCell:

public class EvenOddCell : ViewCell
{
    public Color EvenBackgroundColor
    {
        get => (Color)GetValue(EvenBackgroundColorProperty);
        set => SetValue(EvenBackgroundColorProperty, value);
    }

    public Color OddBackgroundColor
    {
        get => (Color)GetValue(OddBackgroundColorProperty);
        set => SetValue(OddBackgroundColorProperty, value);
    }

    public IList ContainerItemsSource
    {
        get => (IList)GetValue(ContainerItemsSourceProperty);
        set => SetValue(ContainerItemsSourceProperty, value);
    }

    public static BindableProperty EvenBackgroundColorProperty = BindableProperty.Create(
        nameof(EvenBackgroundColor), typeof(Color), typeof(EvenOddCell), default(Color));

    public static BindableProperty OddBackgroundColorProperty = BindableProperty.Create(
        nameof(OddBackgroundColor), typeof(Color), typeof(EvenOddCell), default(Color));

    public static BindableProperty ContainerItemsSourceProperty = BindableProperty.Create(
        nameof(ContainerItemsSource), typeof(IList), typeof(EvenOddCell),
        default(IList), propertyChanged: OnContainerItemsSourcePropertyChanged);

    private static void OnContainerItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if (newValue != null && newValue is INotifyCollectionChanged)
        {
            ((INotifyCollectionChanged)newValue).CollectionChanged += (s, e) =>
            {
                if (e.Action != NotifyCollectionChangedAction.Add)
                {
                    ((EvenOddCell)bindable).OnBindingContextChanged();
                }
            };
        }
    }

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        if (BindingContext != null && ContainerItemsSource is IList)
        {
            var idx = ContainerItemsSource.IndexOf(BindingContext);
            View.BackgroundColor = idx % 2 == 0 ? EvenBackgroundColor : OddBackgroundColor;
        }
    }
}