【问题标题】:Binding to elements of a list in xamarin绑定到 xamarin 中的列表元素
【发布时间】:2020-02-18 21:41:07
【问题描述】:

所以我已经创建了多个标签,我想将每个 label.text 绑定到列表中的一个项目,如代码所示。但似乎我无法通过索引访问项目的值

<StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack0" IsVisible="True">
                        <Label  x:Name="CountRep0" FontSize="18" TextColor="White" Text="{Binding Counter[0]}"/>

                        <Label  x:Name="Objname0" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[0]}"/>

                    </StackLayout>

<StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack1" IsVisible="True">
                        <Label  x:Name="CountRep1" FontSize="18" TextColor="White" Text="{Binding Counter[1]}"/>

                        <Label  x:Name="Objname1" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[1]}"/>

                    </StackLayout>

我有 2 个列表“Objets_de_Commande”和“计数器” 你有什么想法可以让我完成这项工作吗?

【问题讨论】:

  • 我认为你应该使用 ListView
  • Objets_de_Commande 和 Counter 的类型是什么?它们都是 BindingContext 的公共属性吗?
  • @Jason Objets de commande 是一个字符串,另一个是 int。是的,它们都是公开的。
  • 这是我得到的:未处理的异常:System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。参数名称:index a eu lieu
  • 你确定你的两个列表都至少有 2 个元素吗?

标签: c# xamarin xamarin.forms data-binding binding


【解决方案1】:

绝对有可能。

您可以使用您键入的相同代码,使用基于索引的绑定来访问列表项:

<Label  x:Name="Objname0" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[0]}"/>

只需确保您在后面的代码中为页面设置了正确的BindingContext

This 其他帖子显示了如何在代码 (C#) 中进行绑定,但在 XAML 中没有任何不同。

注意:正如其中一个 cmets 所述:您最好查看 ListViewCollectionView

希望这会有所帮助。-

【讨论】:

    【解决方案2】:

    当然,您可以通过索引绑定列表中的一项。例如:

    MainPage.xaml.cs

     public partial class MainPage : ContentPage
    {
        public List<string> Counter { get { return new List<string> { "1", "2" }; } }
    
        public List<string> Objets_de_Commande { get { return new List<string> { "test01", "test02" }; } }
    
        public MainPage()
        {
            InitializeComponent();
    
            BindingContext = this;
        }
    }
    

    MainPage.xaml

     <StackLayout Orientation="Vertical"  >
        <StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack0" IsVisible="True">
            <Label  x:Name="CountRep0" FontSize="18" TextColor="Black" Text="{Binding Counter[0]}"/>
            <Label  x:Name="Objname0" FontSize="18" TextColor="Black" Text="{Binding Objets_de_Commande[0]}"/>
        </StackLayout>
        <StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack1" IsVisible="True">
            <Label  x:Name="CountRep1" FontSize="18" TextColor="Black" Text="{Binding Counter[1]}"/>
    
            <Label  x:Name="Objname1" FontSize="18" TextColor="Black" Text="{Binding Objets_de_Commande[1]}"/>
        </StackLayout>
    </StackLayout>
    

    注意:

    只注意背景和字体颜色,如果它们是相同的颜色,我们看不到结果。 所以我把文字的颜色改成黑色:

    TextColor="Black"
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 2019-06-07
      • 2018-10-19
      • 2016-06-06
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多