![](/img/trans.png)
[英]Xamarin Forms: How to change background from gray on selected item in listview, iOS
[英]Xamarin Forms - Listview on selected item change all background to the same
當您選擇一個項目時,我發現一個錯誤,所有子項背景都因相同的顏色而變化。
在所有元素中,我將屬性BackgroundColor。 這只發生在iOS上
按照代碼示例:
XAML
<ListView
x:Name="ListPainel"
SeparatorColor="#d2d8e2"
SeparatorVisibility="Default"
Margin="0"
ItemsSource="{Binding ListPainel_Source}"
HasUnevenRows="true"
RefreshCommand="{Binding ListPainel_RefreshCommand}"
IsRefreshing="{Binding ListPainel_IsRefreshing}"
>
</ListView>
ViewCell的一部分
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
dynamic temp = BindingContext;
PainelDto painel = (PainelDto)temp;
...
if(painel.HasDetalhes)
{
Button detalhes = new Button()
{
Text="VER DETALHES",
FontSize = 12,
TextColor = Color.FromHex("#4482ff"),
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Start,
HeightRequest = 20,
WidthRequest = 120,
BackgroundColor = Color.DeepPink
};
detalhes.SetBinding(
Button.CommandProperty
, new Binding(
"ViewDetalhesCommand"
, BindingMode.Default
, null
, null
, null
, _viewModelPainel
)
);
box.Children.Add(detalhes);
}
...
View = box;
}
有人知道如何解決這個問題嗎?
這是在iOS中選擇單元格時的默認行為。
我們需要ViewCell
的custom renderer
來禁用該效果。
在iOS項目中創建一個名為NativeiOSCellRenderer
的類。
代碼:
[assembly: ExportRenderer(typeof(ViewCell), typeof(NativeiOSCellRenderer))]
namespace FormsListViewSample.iOS
{
class NativeiOSCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.