Listview 选定项在列表视图中高亮显示Xamarin.Forms
目前,我有一个自定义的Listview 选定项在列表视图中高亮显示Xamarin.Forms,listview,xamarin,xamarin.forms,highlight,selected,Listview,Xamarin,Xamarin.forms,Highlight,Selected,目前,我有一个自定义的CellView模板,我创建该模板是为了有效地使用整个单元格的背景图像,并具有文本覆盖 我通过绝对布局完成了这项工作,并将自定义单元格模板绑定到我的ListView 我的问题是,我似乎不再能够在选择时突出显示单元格 我猜想我的问题是源于占据单元格100%背景的图像(首先将其添加到堆栈布局中,而不是作为正确的背景图像),或者我的自定义ViewCell实现没有正确实现ItemSelection 我的印象是,ListView单元格在默认情况下会有某种选择高亮显示。它使用的是一个默
CellView
模板,我创建该模板是为了有效地使用整个单元格的背景图像,并具有文本覆盖
我通过绝对布局完成了这项工作,并将自定义单元格模板绑定到我的ListView
我的问题是,我似乎不再能够在选择时突出显示单元格 我猜想我的问题是源于占据单元格100%背景的图像(首先将其添加到堆栈布局中,而不是作为正确的背景图像),或者我的自定义
ViewCell
实现没有正确实现ItemSelection
我的印象是,ListView
单元格在默认情况下会有某种选择高亮显示。它使用的是一个默认的列表视图
,而我使用的是一个相对布局,图像外有一小段单元格。这就是我能够突出显示单元格的地方 图像从未实际高亮显示。这就是为什么我怀疑图像是个问题 我想要实现的目标 点击时高亮显示整个单元格图像,并使用某种颜色全部高亮显示,以表明已选中该图像。在共享代码(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;
}
}
在这之后,我就有了绑定的基本访问器,并创建了列表/应用了模板。如果需要,我可以提供,但它们是基本的 Hye。
为此,您必须使用Item Selected属性并将其与变量绑定,然后对其执行所需操作(例如,对列表中的每个项目使用Name属性以选中此项)
我并没有测试过,我想你们会明白的
以下是您的列表:
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}
};
MenuItems=新的ObservableCollection
{
新建MasterPageMenuItemModel{Name=“gistPage”,ImageSource=GistsPageImagePath},
新建MasterPageMenuItemModel{Name=“IssueDashboardPage”,ImageSource=IssueDashboardPageImagePath},
新建MasterPageMenuItemModel{Name=“BookmarksPage”,ImageSource=BookmarksPageImagePath},
新建MasterPageMenuItemModel{Name=“ReportAnIssuePage”,ImageSource=ReportAnIssuePageImagePath}
};
好的,现在您有了Name属性。
然后使用“选定项”将其与您的属性绑定:
<ListView x:Name="MasterPageMenu"
SeparatorColor="Transparent"
AbsoluteLayout.LayoutBounds="{Binding MasterMenuBounds}"
AbsoluteLayout.LayoutFlags="None"
ItemsSource="{Binding MenuItems}"
SelectedItem="{Binding MenuItemSelectedProperty, Mode=TwoWay}">
要在需要时进行检查,可以使用ItemSelected事件。
当它升起时,请将您的可绑定属性与Name属性一起使用,并对其执行您想要的操作