0
votes

I have a scenario like, in a ListView I have two Labels and a custom made horizontal List in each item.

Horizontal List - stack of Labels in a ScrollView with orientation horizontal.

What I need is, I want to refer a Label which is inside the Horizontal List of a particular item of the ListView and make the selected Label from the Horizontal List to Bold. Is there a way to refer a control of ListView item?

Below is the code for filling my 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

1 Answers

0
votes

You can use the ItemTapped to find which item of your listview is getting tapped. To get the text bold you should use a binding on your font. then in your codebehind you can switch the value of that binding and so will your font change. Let me know if this worked for you!