双击自定义列表视图项

时间:2016-12-27 13:55:09

标签: ios listview dynamic xamarin.forms rendering

我的自定义列表视图

<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);
        }
    }

我以这种方式呈现了列表视图

public class CustomListViewRenderer:ListViewRenderer     {         protected override 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();
                }
            }

        }
    }
}

它在第一次点击时工作正常,但是当我第二次在同一项目上拼错时,背景设置为默认背景。 请告诉我错误的地方。

1 个答案:

答案 0 :(得分:0)

您可以使用this了解用户是否双击项目&amp;如果用户双击任何特定项目清除所选项目如下:

((ListView)sender).SelectedItem = null;