首页 > 解决方案 > DataTemplate 中的 Xamarin.Forms CollectionView 项目“背后”是什么(关于背景颜色)?

问题描述

我有一个带圆角的框架作为我的 DataTemplate 的最外层容器......但我选择的项目仍然突出显示框架“背后”的背景......请参阅: 所选项目背景颜色示例

我假设我应该向 VisualStatemanager 添加一个 setter,但我应该针对什么元素来改变它?

这是视图的摘要:

public class MyView : ContentPage
{
    public MyView()
    {
        var mainStackLayout = new StackLayout();
        Content = mainStackLayout;
        
        var cv = new CollectionView
        {
            ItemSizingStrategy = ItemSizingStrategy.MeasureFirstItem,
            VerticalOptions = LayoutOptions.FillAndExpand,
            SelectionMode = SelectionMode.Multiple,
            ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical) { ItemSpacing = 8 }
            
        };
        cv.SetBinding(CollectionView.ItemsSourceProperty, "MyList"); 
        cv.SetBinding(CollectionView.SelectedItemsProperty, "MySelectedItems"); 
        cv.SetBinding(CollectionView.SelectionChangedCommandProperty, "MySelectionChangedCommand"); 

        cv.ItemTemplate = new DataTemplate(() =>
        {
            BackgroundColor = Color.HotPink; //Does nothing...?

            var outterFrame = new Frame() 
            { 
                IsClippedToBounds = true,
                CornerRadius = 25,
                Padding = 0,
                Margin = new Thickness(3),
                BorderColor = Color.Gray
            };
            
            
            var vsg = new VisualStateGroup() { Name = "vsg" };
            var vsNormal = new VisualState { Name = "Normal" };
            var vsSelected = new VisualState { Name = "Selected" };
            vsNormal.Setters.Add(new Setter
            {
                Property = Frame.BackgroundColorProperty,
                Value = Color.White
            });
            vsSelected.Setters.Add(new Setter
            {
                Property = Frame.BackgroundColorProperty,
                Value = Color.LightBlue
            });
            
            ////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////////
            // I assume I need to add another setter here.... but what's the target?    
            //  
            //vsSelected.Setters.Add(new Setter
            //{
            //    Property = _________.BackgroundColorProperty,
            //    Value = Color.Transparent
            //});               
            //
            ////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////////
            
            vsg.States.Add(vsNormal);
            vsg.States.Add(vsSelected);
            VisualStateManager.GetVisualStateGroups(fOutter).Add(vsg);

            var grid = new Grid
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Margin = new Thickness(0),
                RowSpacing = 0
            };

            grid.RowDefinitions.Add(new RowDefinition { blah, blah, blah...  });
            grid.RowDefinitions.Add(new RowDefinition { blah, blah, blah...  });
            grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah...   });
            grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah...   });
            grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah...   });

            var icon = new Image{ blah, blah, blah... };
            var moreStuffForTheItem = blah, blah, blah...
            
            outterFrame.Content = grid;             
            return outterFrame;

        });  //end cv.ItemTemplate 

        mainStackLayout.Children.Add(cv);
    }
}

标签: c#xamarin.formsdatatemplatevisualstatemanager

解决方案


您可以尝试将默认选择高亮颜色设置为透明。

对于 Android,在 android 项目中,在您的主题样式 ( Resources/values/styles)中添加以下代码

<item name="android:colorActivatedHighlight">@android:color/transparent</item>

对于 ios,您需要创建一个自定义的 viewcell 渲染器。

using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer (typeof(ViewCell),typeof(MyAPP.iOS.CustomAllViewCellRendereriOS))]
namespace MyAPP.iOS
{
  public class CustomAllViewCellRendereriOS : ViewCellRenderer
  {
    public override UIKit.UITableViewCell GetCell (Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
    {
        var cell =  base.GetCell (item, reusableCell, tv);
        if (cell != null)
            cell.SelectionStyle = UIKit.UITableViewCellSelectionStyle.None;
        return cell;
    }
  }  
}

推荐阅读