Listview

时间:2017-08-31 10:13:42

标签: listview xamarin xamarin.forms

我有一个listView和item selected event.Here我想让我的第一个项目默认选择在view上显示列表。后选择其他项目它应该正常工作。

        ListView listView = new ListView
        {
            ItemsSource = staticSpListDetailses,

            ItemTemplate = new DataTemplate(() =>
            {

                var nameLabel = new Label();
                nameLabel.FontSize = 18;
                nameLabel.WidthRequest = 180;
                nameLabel.HorizontalTextAlignment = TextAlignment.Start;
                nameLabel.FontAttributes = FontAttributes.Bold;
                nameLabel.SetBinding(Label.TextProperty, "Name");

                var subtitlelabel = new Label();
                subtitlelabel.FontSize = 16;
                subtitlelabel.HorizontalTextAlignment = TextAlignment.Start;
                subtitlelabel.TextColor = Color.Silver;
                subtitlelabel.SetBinding(Label.TextProperty, "SubTitle");

                var arrowmarklabel = new Label();
                arrowmarklabel.FontSize = 20;
                arrowmarklabel.HorizontalOptions = LayoutOptions.EndAndExpand;
                arrowmarklabel.VerticalOptions = LayoutOptions.Center;
                arrowmarklabel.TextColor = Color.Black;
                arrowmarklabel.Text = ">";
                arrowmarklabel.FontAttributes = FontAttributes.Bold;

                var arrowmarklabel1 = new Label();
                arrowmarklabel1.WidthRequest = 90;

                var insideStackLayout = new StackLayout
                {
                    Orientation = StackOrientation.Vertical,
                    VerticalOptions = LayoutOptions.Center,
                    Spacing = 5,
                    Children =
                    {
                        nameLabel,subtitlelabel
                    }
                };

                var image = new Image();
                image.Scale = 0.8;
                image.SetBinding(Image.SourceProperty, "ImageSourceUrl");

                return new ViewCell
                {
                    View = new StackLayout
                    {
                        Padding = new Thickness(10,0, 5, 0),
                        Orientation = StackOrientation.Horizontal,
                        Children = {

                            new StackLayout {
                                VerticalOptions = LayoutOptions.Center,
                                Orientation=StackOrientation.Horizontal,

                                Children = {
                                    image,insideStackLayout,arrowmarklabel
                                }
                            }
                        }
                    }
                };
            })
        };

这是我的Itemselected事件:

listView.ItemSelected += (sender, e) =>
        {
            var data = ((ListView)sender).SelectedItem as StaticListDetails;
            Detail = data.page;               
        };

我的代码需要进行哪些更改才能在列表视图中将第一个项目选为默认值。

1 个答案:

答案 0 :(得分:0)

listView.SelectedItem = staticSpListDetailses?.FirstOrDefault()