我的 ListView 包含名为“Item”的对象列表 我希望单击某个项目后,将其背景颜色更改为不同的颜色。 我的问题是我无法访问特定的单元格。我可以使用
list.selectedItem
访问该项目,但不能使用包含他的单元格
this.list = new ListView
{
ItemsSource = new ObservableCollection<Item>(Lists.Items),
ItemTemplate = new DataTemplate(() =>
{
Label nameLabel = Lists.settings.CreateLabel();
Label descriptionLabel = Lists.settings.CreateLabel();
Button addButton = new Button
{
Text = "+",
FontSize = 16,
BackgroundColor = Color.FromHex(Settings.LightModeColors[0]),
TextColor = Colors.Black,
};
Button deductButton = new Button
{
Text = "-",
FontSize = 16,
BackgroundColor = Color.FromHex(Settings.DarkModeColors[0]),
TextColor = Colors.White,
};
Label quantityLabel=Lists.settings.CreateLabel("0");
nameLabel.SetBinding(Label.TextProperty, nameof(Item.Name));
descriptionLabel.SetBinding(Label.TextProperty, nameof(Item.Description));
addButton.Clicked += async (sender, args) =>
{
quantityLabel.Text = "" + (int.Parse(quantityLabel.Text) + 1);
Debug.WriteLine(args.GetType());
AddItem("id");
};
deductButton.Clicked += async (sender, args) =>
{
if (quantityLabel.Text != "0")
{
quantityLabel.Text = "" + (int.Parse(quantityLabel.Text) - 1);
DeductItem(nameof(Item.Id));
}
};
Grid templateLayout = new Grid
{
RowDefinitions =
{
new RowDefinition{Height=new GridLength(1,GridUnitType.Star) },
},
ColumnDefinitions =
{
new ColumnDefinition{Width=new GridLength(3,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(2,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
},
};
templateLayout.Add(nameLabel, 0, 0);
templateLayout.Add(descriptionLabel, 1, 0);
templateLayout.Add(addButton, 2, 0);
templateLayout.Add(quantityLabel, 3, 0);
templateLayout.Add(deductButton, 4, 0);
return new ViewCell { View = templateLayout };
})
};
list.ItemTapped += OnItemTapped;
private void OnItemTapped(object sender, ItemTappedEventArgs e)
{
//list.SelectedItem = null;
Debug.WriteLine(sender.GetType());
Debug.WriteLine(((ListView)sender).SelectedItem.GetType());
Debug.WriteLine(e.GetType());
Debug.WriteLine(e.ItemIndex.GetType());
Debug.WriteLine(e.Item.GetType());
}
尝试调试以在listview中找到某种类型的View对象
您可以尝试使用 Visual State 来更改 SelectedItem 的背景颜色,我认为这是最简单的方法。但在这里我建议使用 CollectionView 而不是
ListView
,因为 CollectionView
旨在为 ListView 提供更灵活、更高效的替代方案。 (由于某种原因,视觉状态在 ListView 中不起作用)
这是在代码后面添加视觉状态的示例。
this.list = new CollectionView()
{
ItemsSource = items,
SelectionMode = SelectionMode.Single,
ItemTemplate = new DataTemplate(() =>
{
Label nameLabel = new Label();
Label descriptionLabel = new Label();
...
...
//Define Visual States here
var vsgs = new VisualStateGroupList();
var vsg = new VisualStateGroup() { Name = "CommonStates" };
var vs_normal = new VisualState { Name = "Normal" };
var vs_selected = new VisualState { Name = "Selected" };
vs_normal.Setters.Add(new Setter { Property = BackgroundColorProperty, Value = Colors.White });
vs_selected.Setters.Add(new Setter { Property = BackgroundColorProperty, Value = Colors.Green });
vsg.States.Add(vs_normal);
vsg.States.Add(vs_selected);
vsgs.Add(vsg);
Grid templateLayout = new Grid
{
...
};
templateLayout.Add(nameLabel, 0, 0);
...
templateLayout.Add(deductButton, 4, 0);
//Set VisualStates on the Grid
VisualStateManager.SetVisualStateGroups(templateLayout, vsgs);
return templateLayout;
})
};
希望有帮助!