我在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;
}
}
}
答案 0 :(得分: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;
}
答案 1 :(得分: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