单击时设置HVScrollView项目的背景颜色

时间:2019-03-24 13:30:11

标签: xamarin.forms

我已经在Xamarin.Forms中实现了HVScrollView自定义渲染器,如下所示,并且正在尝试设置点击/单击时所选项目的背景颜色,但不知道该怎么做。

我无法确定是否需要向自定义渲染器中添加一些内容以使其显示所选项目,还是应该在标记中对其进行设置。

请帮助

public class HVScrollGridView : Grid
{
    private ICommand _innerSelectedCommand;
    private readonly ScrollView _scrollView;
    private readonly StackLayout _itemsStackLayout;

    public event EventHandler SelectedItemChanged;

    public StackOrientation ListOrientation { get; set; }

    public double Spacing { get; set; }

    public static readonly BindableProperty SelectedCommandProperty =
        BindableProperty.Create("SelectedCommand", typeof(ICommand), typeof(HVScrollGridView), null);

    public static readonly BindableProperty ItemsSourceProperty =
        BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HVScrollGridView), default(IEnumerable<object>), BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);

    public static readonly BindableProperty SelectedItemProperty =
        BindableProperty.Create("SelectedItem", typeof(object), typeof(HVScrollGridView), null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);

    public static readonly BindableProperty ItemTemplateProperty =
        BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(HVScrollGridView), default(DataTemplate));

    public ICommand SelectedCommand
    {
        get { return (ICommand)GetValue(SelectedCommandProperty); }
        set { SetValue(SelectedCommandProperty, value); }
    }

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var itemsLayout = (HVScrollGridView)bindable;
        itemsLayout.SetItems();
    }

    public HVScrollGridView()
    {
        _scrollView = new ScrollView();

        _itemsStackLayout = new StackLayout
        {
            BackgroundColor = BackgroundColor,
            Padding = Padding,
            Spacing = Spacing,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };

        _scrollView.BackgroundColor = BackgroundColor;
        _scrollView.Content = _itemsStackLayout;
        Children.Add(_scrollView);
    }

    protected virtual void SetItems()
    {
        _itemsStackLayout.Children.Clear();
        _itemsStackLayout.Spacing = Spacing;

        _innerSelectedCommand = new Command<View>(view =>
        {
            SelectedItem = view.BindingContext;
            SelectedItem = null; // Allowing item second time selection
        });

        _itemsStackLayout.Orientation = ListOrientation;
        _scrollView.Orientation = ListOrientation == StackOrientation.Horizontal
            ? ScrollOrientation.Horizontal
            : ScrollOrientation.Vertical;

        if (ItemsSource == null)
        {
            return;
        }

        foreach (var item in ItemsSource)
        {
            _itemsStackLayout.Children.Add(GetItemView(item));
        }

        _itemsStackLayout.BackgroundColor = BackgroundColor;
        SelectedItem = null;
    }

    protected virtual View GetItemView(object item)
    {
        var content = ItemTemplate.CreateContent();
        var view = content as View;

        if (view == null)
        {
            return null;
        }

        view.BindingContext = item;

        var gesture = new TapGestureRecognizer
        {
            Command = _innerSelectedCommand,
            CommandParameter = view
        };

        AddGesture(view, gesture);

        return view;
    }

    private void AddGesture(View view, TapGestureRecognizer gesture)
    {
        view.GestureRecognizers.Add(gesture);

        var layout = view as Layout<View>;

        if (layout == null)
        {
            return;
        }

        foreach (var child in layout.Children)
        {
            AddGesture(child, gesture);
        }
    }

    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var itemsView = (HVScrollGridView)bindable;

        if (newValue == oldValue && newValue != null)
        {
            return;
        }

        itemsView.SelectedItemChanged?.Invoke(itemsView, EventArgs.Empty);

        if (itemsView.SelectedCommand?.CanExecute(newValue) ?? false)
        {
            itemsView.SelectedCommand?.Execute(newValue);
        }
    }

    public static explicit operator ListView(HVScrollGridView v)
    {
        throw new NotImplementedException();
    }
}

0 个答案:

没有答案