簡體   English   中英

刪除一行后更新 ListView DataTemplate

[英]Update ListView DataTemplate after removing one row

我在使用 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>

我的數據模板選擇器 class:

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 只能通過添加/刪除進行修改。 我沒有嘗試插入項目。 我正在開發的這個功能不需要它。

任何想法?

快速演示

我剛剛解決了這個問題。 這是我所做的:

我創建了ViewCell class 的自定義,並添加了三個可綁定屬性ContainerItemsSourceEvenBackgroundColorOddBackgroundColor ,然后我重寫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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM