【发布时间】:2017-05-11 19:39:59
【问题描述】:
我的自定义列表视图
<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 { 受保护的覆盖无效 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();
}
}
}
}
}
它在第一次点击时工作正常,但是当我第二次点击同一个项目时,背景被设置为默认背景。 请让我知道我要去哪里出错。
【问题讨论】:
标签: ios listview dynamic xamarin.forms rendering