如何将背景颜色更改为ListView中的项目?

问题描述 投票:0回答:2

我在ListView中使用CustomCell作为项目。 CustomCell在DataTemplate中用作TextCell。如何在选择该项目时更改backgroundColor?物品是从api填充所以它的dinamic。我试图使用Focused事件,但它不起作用。感谢帮助

public class CustomCell : ViewCell
{
    StackLayout cellWrapper = new StackLayout();
    public CustomCell()
    {
        StackLayout titleStack = new StackLayout();
        StackLayout horizontalLayout = new StackLayout();
        Label Tittle = new Label();
        Label Detail = new Label();
        Label Detail2 = new Label();

        Tittle.SetBinding(Label.TextProperty, "Title");
        Detail.SetBinding(Label.TextProperty, "Datail");
        Detail2.SetBinding(Label.TextProperty, "Detail2");

        cellWrapper.BackgroundColor = Color.FromHex("#fff");
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        cellWrapper.Padding = 10;

        Tittle.TextColor = Color.FromHex("#000");
        Detail.TextColor = Color.FromHex("#a59f9f");
        Tittle.FontSize = 20;


        titleStack.Children.Add(Tittle);
        horizontalLayout.Children.Add(Detail);
        horizontalLayout.Children.Add(Detail2);

        cellWrapper.Children.Add(titleStack);
        cellWrapper.Children.Add(horizontalLayout);
        cellWrapper.Focused += CellWrapper_Focused;

        View = cellWrapper;
    }

    private void CellWrapper_Focused(object sender, FocusEventArgs e)
    {
        if (e.IsFocused)
        {
            cellWrapper.BackgroundColor = Color.Blue;
        }
    }
}
c# xamarin colors xamarin.forms
2个回答
0
投票

Android的

var cell = base.GetCellCore(item, convertView, parent, context);
var listView = parent as Android.Widget.ListView;
if (listView != null)
{
    // Disable native cell selection color style - set as *Transparent*
    listView.SetSelector(Android.Resource.Color.Transparent);
    listView.CacheColorHint = Android.Graphics.Color.Transparent;
}

IOS

var cell = base.GetCell(item, reusableCell, tv);
if (cell != null)
{
    // Disable native cell selection color style - set as *Transparent*
    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
}

0
投票

我认为你的模型中可以有一个“Selected”布尔属性。

当SelectedItem上升时,您可以将“Selected”属性设置为“true”。

将Background color属性绑定到“Selected”属性...然后,使用IValueConverter,将布尔值“true”值转换为“selected color”,将“false”值转换为“unselected”颜色。

记住在选择新项目时取消选择上一个选定项目。

例如,这可以是您的模型

namespace TestSelectedItemInListView
{
    [ImplementPropertyChanged]
    public class MyModel
    {
        public string Name { get; set; }
        public string Surname { get; set; }

        public bool Selected { get; set; }

        public MyModel()
        {
        }
    }
}

这个转换器

public class SelectedToColorConverter : IValueConverter
{

    #region IValueConverter implementation

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            if ((Boolean)value)
                return Color.Red;
            else
                return Color.Black;
        }
        return Color.Black;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

这是ViewModel中的SelectedItem属性

    public MyModel SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (_selectedItem != null)
                _selectedItem.Selected = false;

            _selectedItem = value;

            if (_selectedItem != null)
            {
                _selectedItem.Selected = true;
                MessagingCenter.Send<MasterPageViewModel, string>(this, "Detail", _selectedItem.Name);
            }
        }
    }

你可以找到一个回购TestSelectedItemInListView

© www.soinside.com 2019 - 2024. All rights reserved.