简体   繁体   English

如何在列表视图的项目中引用控件或视图-Xamarin Forms

[英]How do I refer a control or view in an item of listview - xamarin forms

I have a scenario like, in a ListView I have two Labels and a custom made horizontal List in each item. 我有一个场景,例如在ListView中,每个项目中都有两个Label和一个自定义的水平List。

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. 我需要的是,我想引用一个位于ListView特定项目的“水平列表”内的标签,然后将“水平列表”中的选定标签设置为粗体。 Is there a way to refer a control of ListView item? 有没有一种方法可以引用ListView项目的控件?

Below is the code for filling my 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,
                }
            }
        };
    }
)};

You can use the ItemTapped to find which item of your listview is getting tapped. 您可以使用ItemTapped查找被点击的列表视图中的项目。 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! 让我知道这是否对您有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ListView时,如何在Xamarin Forms中将自定义控件绑定设置为父视图模型属性? - How can I set custom control binding to parent view model property in Xamarin Forms when using a ListView? 如何使用 MVVM 和 Xamarin.Forms ListView ItemSelected 事件来检索所选项目的绑定属性? - How do I use MVVM and Xamarin.Forms ListView ItemSelected event to to retrieve a bound property of the item selected? 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 如何引用WPF嵌套ListView中的控件? - How do I refer to a control in a WPF nested ListView? 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML 如何从Xamarin Forms ListView中删除项目? - How to remove an item from a Xamarin Forms ListView? Xamarin Forms,如何使用 ListView 收藏项目 - Xamarin Forms, How to favorite Item using ListView Xamarin.Forms 如何禁用 ListView 上的单击动画 - Xamarin.Forms How do I disable click animation on ListView 如何在具有ReactiveUI和Xamarin表单的ListView ItemTemplate中使用ViewModelViewHost? - How do I use ViewModelViewHost in a ListView ItemTemplate with ReactiveUI and Xamarin Forms? 如何为Xamarin Forms ListView创建消息框? - How do I create a message box for Xamarin Forms ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM