【问题标题】:How do I refer a control or view in an item of listview - xamarin forms如何在列表视图项中引用控件或视图 - xamarin 表单
【发布时间】:2017-05-17 11:48:24
【问题描述】:

我有一个场景,例如,在 ListView 中,我有两个标签和每个项目中的自定义水平列表。

水平列表 - 滚动视图中的标签堆栈,方向为水平。

我需要的是,我想引用一个位于 ListView 特定项目的水平列表内的标签,并将水平列表中的选定标签设为粗体。有没有办法引用 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,
                }
            }
        };
    }
)};

【问题讨论】:

    标签: c# listview xamarin.forms


    【解决方案1】:

    您可以使用 ItemTapped 来查找您的列表视图中的哪个项目正在被点击。要使文本加粗,您应该在字体上使用绑定。然后在您的代码隐藏中,您可以切换该绑定的值,您的字体也会改变。 让我知道这是否适合您!

    【讨论】:

    • 抱歉回复晚了..我尝试了 ItemTapped/ItemSelected,但是当我点击水平列表时它们没有被触发..我想你可能弄错了..我想参考一个水平列表内部的标签(我提到过)..但不是 Listview 项目的标签..
    • 啊我现在明白你的问题了,我想。所以第三个标签,你添加点击手势的地方,应该响应被点击,对吗?这基本上是我实现我的tappgesuture的方式。 var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += OnTap; ChangePicture.GestureRecognizers.Add(tapGestureRecognizer); chagepicture 是我的标签,在 OnTap 中我处理需要完成的逻辑(在我的情况下是导航输入
    • 是的,你现在挡住我的路了.. 是的,我已经实现了点击手势,当我点击它时,我已经在标签中获取文本.. 但我想要从水平方向选择标签列表也要加粗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多