gpt4 book ai didi

c# - 更改背景颜色 selecteditem Listview

转载 作者:太空狗 更新时间:2023-10-30 00:07:03 33 4
gpt4 key购买 nike

我对 Xamarin.Forms 中的 ListView 有疑问将我的 ListView 与一些项目成功绑定(bind),但我想更改所选单元格的背景颜色我如何在 Xamarin.Forms 中执行此操作

我利用

var cell = DataTemplate(typeof(ImageCell));

ListView listView = new ListView
{
SeparatorColor = Color.Green,
ItemsSource = ListlvData,
ItemTemplate = cell, // Set the ImageCell to the item templatefor the listview
};

最佳答案

编辑 2:

有时候,如果我遇到奇怪的问题,我的 ViewCellBackgroundColor 永远不会变回原来的颜色,所以我开始这样做来改变颜色:

cell.Tapped += async (sender, args) => {
cell.View.BackgroundColor = Color.Red;

#pragma warning disable 4014 //These pragma's are only needed if your Tapped is being assigned an async anonymous function and muffles the compiler warning that you did not await Task.Run() which you do not want to fire and forget it

Task.Run(async () => { //Change the background color back after a small delay, no matter what happens
await Task.Delay(300); //Or how ever long to wait

Device.BeginInvokeOnMainThread(() => cell.View.BackgroundColor = Color.Default); //Turn it back to the default color after your event code is done
});

#pragma warning restore 4014

await OnListViewTextCellTapped(cell); //Run your actual `Tapped` event code

};

编辑:

要将以下代码添加到 ListView.DataTemplate,您需要执行如下操作:

ListView listView = new ListView {
SeparatorColor = Color.Green,
ItemsSource = ListlvData
};

listView.ItemTemplate = new DataTemplate(() => {
ViewCell cell = new ViewCell();

cell.Tapped += (sender, args) => {
cell.View.BackgroundColor = Color.Red;
OnListViewTextCellTapped(cell); //Run your actual `Tapped` event code
cell.View.BackgroundColor = Color.Default; //Turn it back to the default color after your event code is done
};

cell.View = new Image();

return cell;
});

要更改 Tapped 的背景颜色,您需要在其中使用 ViewCellImage 控件,因为 ImageCell 默认不支持 BackgroundColor

我在 ViewCell 中放置了一个 StackLayout 但随后在 Tapped 事件中,我更改了 ViewCell.ViewBackgroundColor,像这样:

ViewCell cell = new ViewCell();

cell.Tapped += (sender, args) => {
cell.View.BackgroundColor = Color.Red;
OnListViewTextCellTapped(cell); //Run your actual `Tapped` event code
cell.View.BackgroundColor = Color.Default; //Turn it back to the default color after your event code is done
};

关于c# - 更改背景颜色 selecteditem Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169851/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com