[英]Removed background color when Double click on custom list view item
我的自定義列表視圖
<common:CustomListView x:Name="MenuListView" Margin="0,2,0,0" ItemsSource="{Binding Menutems}"
SelectedItemBackgroundColor="{x:Static resources:Colors.HikeColor}" ItemBackgroundColor="White"
RowHeight="70" SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}" BackgroundColor="Transparent" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="0" HeightRequest="70" Spacing="0">
<Label Text="{Binding .}" FontSize="16" FontFamily="{x:Static resources:Fonts.HikeDefaultFont}" VerticalOptions="CenterAndExpand" Margin="40, 10, 10, 10" TextColor="{x:Static resources:Colors.HeaderTextColor}"/>
<BoxView HeightRequest="0.5" VerticalOptions="End" HorizontalOptions="FillAndExpand" BackgroundColor="{x:Static resources:Colors.BordersColor}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</common:CustomListView>
查看型號代碼
private string _SelectedMenuItem { get; set; } = "GENERAL";
public string SelectedMenuItem
{
get
{
return _SelectedMenuItem;
}
set
{
_SelectedMenuItem = value;
MenuItemSelected(value);
}
}
我以這種方式呈現了列表視圖
公共類CustomListViewRenderer:ListViewRenderer {受保護的重寫void OnElementChanged(ElementChangedEventArgs e){base.OnElementChanged(e);
var view = (CustomListView)Element;
if (Control != null && view != null)
{
foreach (var cell in Control.VisibleCells)
{
if (cell.Selected)
{
cell.BackgroundColor = view.SelectedItemBackgroundColor.ToUIColor();
}
else {
cell.BackgroundColor = view.ItemBackgroundColor.ToUIColor();
}
}
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var control = (UITableView)Control;
var view = (CustomListView)Element;
if (e.PropertyName == "SelectedItem")
{
if (control != null && view != null)
{
foreach (var cell in Control.VisibleCells)
{
cell.BackgroundColor = view.ItemBackgroundColor.ToUIColor();
}
var ind = control.IndexPathForSelectedRow;
if (ind != null)
{
control.CellAt(ind).BackgroundColor = view.SelectedItemBackgroundColor.ToUIColor();
}
}
}
}
}
它在第一次單擊時工作正常,但是當我第二次在同一項目上單擊時,背景被設置為默認背景。 請讓我知道我要去哪里。
您可以使用它來理解手勢,無論用戶是雙擊項目還是用戶雙擊任何特定項目,清除選定項目,如下所示:
((ListView)sender).SelectedItem = null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.