如何引用listview项目中的控件或视图 - xamarin表单

时间:2017-05-17 11:48:25

标签: c# listview xamarin.forms

我有一个方案,例如ListView我有两个标签,每个项目都有自定义水平列表。

水平列表 - ScrollView中的标签堆栈,方向水平。

我需要的是,我想引用一个位于ListView特定项目的水平列表内的Label,并将选定的Label从水平列表变为粗体。有没有办法引用ListView项的控件?

以下是填写我的ListView

的代码
myListView = new ListView
{
    // Source of data items.
    ItemsSource = itemsource,
    HasUnevenRows = true,
    RowHeight = -1,

    ItemTemplate = new DataTemplate(() =>
    {
        Label label1 = new Label()
        {
            TextColor = Color.Black,
            HorizontalTextAlignment = TextAlignment.Start,
            FontSize = Device.GetNamedSize(NamedSize.Small, new Label())
        };
        label1.SetBinding<LVItem>(Label.TextProperty, indexer => indexer.Name);

        Label label2 = new Label()
        {
            TextColor = Color.Black,
            HorizontalTextAlignment = TextAlignment.Start,
            FontSize = Device.GetNamedSize(NamedSize.Small, new Label())
        };
        label2.SetBinding<LVItem>(Label.TextProperty, indexer => indexer.SelectedNum);

    //horizontal list
        StackLayout sLayout = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
        };

        for (int i = 0; i<itemLst.Count; i++)
        {
            Label label3 = new Label()
            {
                HorizontalTextAlignment = TextAlignment.Center,
                TextColor = Color.Black,
                FontSize = Device.GetNamedSize(NamedSize.Medium, new Label())
            };
            label3.Text = itemLst[i];

            gestureRecognizer = new TapGestureRecognizer
            {
                Command = new Command(TapL_Tapped),
                CommandParameter = label3,
            };

            label3.GestureRecognizers.Add(gestureRecognizer);

            sLayout.Children.Add(label3);
        }

        ScrollView scroll = new ScrollView
        {
            Orientation = ScrollOrientation.Horizontal,
            Content = new StackLayout
            {
                Children =
                {
                    sLayout
                }
            }
        };


        AbsoluteLayout layout = new AbsoluteLayout();
        AbsoluteLayout.SetLayoutFlags(label1, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds(label1, new Rectangle(0.2, 0.2, 0.8, 0.25));

        AbsoluteLayout.SetLayoutFlags(scroll, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds(scroll, new Rectangle(0.3, 0.6, 0.8, 0.2));

        AbsoluteLayout.SetLayoutFlags(label2, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds(label2, new Rectangle(1.1, 0.3, 0.5, 0.2));

        layout.Children.Add(label1);
        layout.Children.Add(scroll);
        layout.Children.Add(label2);

        return new ViewCell
        {
            View = new StackLayout
            {
                Children =
                {
                    layout,
                }
            }
        };
    }
)};

1 个答案:

答案 0 :(得分:0)

您可以使用ItemTapped查找列表视图中的哪个项目。要使文本变为粗体,您应该在字体上使用绑定。然后在你的代码隐藏中你可以切换该绑定的值,所以你的字体也会改变。 如果这对你有用,请告诉我!