首页 文章

所选项目在ListView中突出显示Xamarin.Forms

提问于
浏览
4

目前我有一个自定义 CellView 模板,我创建该模板是为了有效地使用整个单元格的背景图像,并具有文本覆盖 .

我通过绝对布局完成了这项工作,并将自定义单元格模板绑定到我的 ListView .
我的问题是,我似乎不再在选择时突出显示一个单元格 .

我猜测我的问题源于图像占据了单元格背景的100%(通过首先将其添加到堆栈布局而不是正确的背景图像),或者我的自定义 ViewCell 实现没有正确实现 ItemSelection .

我的印象是 ListView 单元格默认会有某种选择突出显示 . 它正在使用默认的 ListView ,而我正在使用相对布局,其中一小部分单元格位于图像之外 .
这是我能够突出细胞的地方 .

图像从未实际突出显示 . 所以这就是为什么我怀疑图像是问题的原因 .

What I Want to Accomplish

在点击时突出显示整个单元格图像,并使用某种颜色突出显示所有单元格图像以指示已选中它 . 在共享代码(PCL)中 .

到目前为止,我试图创建一个不可见的 BoxView ,其中一些透明度在点击时变得可见;但是我无法弄清楚如何使用 ItemSelected() 实现这一点 . 我不是要求编码解决方案,而是要求指向正确的方向;但是,我不会对编码解决方案提出异议 .

public CustomViewCell()
    {
        var someLabel= new Label();
        someLabel.SetBinding (Label.TextProperty, "someLabel");

        var someImage = new Image () {
            Aspect = Aspect.AspectFill,
        };
        someImage.SetBinding (Image.SourceProperty, "ImageSource");

        AbsoluteLayout.SetLayoutFlags (someImage, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds (someImage, new Rectangle (0f, 0f, 1f, 1f));

        AbsoluteLayout.SetLayoutFlags (someLabel, AbsoluteLayoutFlags.PositionProportional);
        AbsoluteLayout.SetLayoutBounds (someLabel, 
            new Rectangle (0.1, 0.85, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)
        );

        AbsoluteLayout theLayout = new AbsoluteLayout {
            HeightRequest = 50,
            BackgroundColor = Color.Black
        };

        theLayout.Children.Add (someImage);
        theLayout.Children.Add (someLabel);

        this.View = theLayout;
    }
}

在此之后,我只有绑定的基本访问器,并创建了List /应用模板 . 如果需要我可以提供,但它们是基本的 .

1 回答

  • 0

    惠 . 为此,您必须使用Item Selected Property并将其与您的变量绑定,之后执行您想要的操作(例如,使用Name属性与列表中的每个项目进行检查) .

    我没有测试过,我想你会 grab 这个想法 .

    这是你的清单:

    MenuItems = new ObservableCollection<MasterPageMenuItemModel>
                {
                    new MasterPageMenuItemModel {Name = "GistsPage", ImageSource = GistsPageImagePath},
                    new MasterPageMenuItemModel {Name = "IssueDashboardPage", ImageSource = IssueDashboardPageImagePath},
                    new MasterPageMenuItemModel {Name = "BookmarksPage", ImageSource = BookmarksPageImagePath},
                    new MasterPageMenuItemModel {Name = "ReportAnIssuePage", ImageSource = ReportAnIssuePageImagePath}
                };
    

    好的,现在你有了Name属性 . 然后使用“Selected Item”将其与您的属性绑定:

    <ListView x:Name="MasterPageMenu"
                      SeparatorColor="Transparent"
                      AbsoluteLayout.LayoutBounds="{Binding MasterMenuBounds}"
                      AbsoluteLayout.LayoutFlags="None"
                      ItemsSource="{Binding MenuItems}"
                      SelectedItem="{Binding MenuItemSelectedProperty, Mode=TwoWay}">
    

    要在需要时进行检查,可以使用ItemSelected事件 . 当它引发使用你的可绑定属性和Name属性时,用它做你想要的 .

相关问题