【问题标题】:Removed background color when Double click on custom list view item双击自定义列表视图项时删除背景颜色
【发布时间】: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


    【解决方案1】:

    您可以使用this 来了解用户是否双击该项目以及用户是否双击任何特定项目清除所选项目的手势,如下所示:

    ((ListView)sender).SelectedItem = null; 
    

    【讨论】:

    • 我使用的是 MVVM 结构,我的 Listview SelectedItem 绑定模式是 TwoWay。如果我将根据您的建议自动编写代码,我的有界属性也将为空。所以我不能写这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2016-01-15
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多