如何更改串列視圖中特定專案的顏色?Ex(具有活動和非活動狀態的用戶串列視圖)
活動專案背景或邊框顏色將設定為綠色,非活動設定為紅色。
uj5u.com熱心網友回復:
從我的角度來看,最簡單的方法是使用視圖單元格點擊事件更改所選 ViewCell 的顏色,如下所示:
public partial class PageList : ContentPage
{
ViewCell lastCell;
public PageList()
{
InitializeComponent();
listView.ItemsSource = new List<Contact>
{
new Contact { Name = "JP Morgan"},
new Contact { Name ="Andrew Carnegie"},
new Contact{ Name="Steve Jobs"}
};
}
private void ViewCell_Tapped(object sender, EventArgs e)
{
if (lastCell != null)
lastCell.View.BackgroundColor = Color.Red;
var viewCell = (ViewCell)sender;
if (viewCell.View != null)
{
viewCell.View.BackgroundColor = Color.Green;
lastCell = viewCell;
}
}
}
下面是 xaml 中的串列視圖,請注意,我將 stacklayout 的背景顏色設定為紅色,因為這是默認的非活動顏色。
<ListView x:Name="listView"
SeparatorColor="Aqua">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell Tapped="ViewCell_Tapped" >
<StackLayout Background="red">
<Label Text="{Binding Name}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

uj5u.com熱心網友回復:
你應該看看'VisualStateManager': https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual-state-manager
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424299.html
