【问题标题】:Array of string not binding to the listview字符串数组未绑定到列表视图
【发布时间】:2020-05-07 08:58:10
【问题描述】:

我正在尝试将数组绑定到列表视图。它没有约束力并显示为空白。我使用模型类来放置数组。

查看模型:

public class RunningLateOptions
    {
      public string[] runningLateOptions = new[] { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours" };
        public string[] runningLateOption
        {
           get{ return runningLateOptions; }
        }
    }

XAML 代码:

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
     <ListView.ItemTemplate>
        <DataTemplate>
          <Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

我无法理解这有什么问题。帮帮我。

【问题讨论】:

  • 这个字符串数组在哪里?在您的 ViewModel 或 Xaml.cs 中?
  • 放置在视图模型中的字符串数组
  • 你能快点给我看一下你的VM代码吗
  • 我提供我的虚拟机。请检查代码。
  • 以下解决方案对您有用吗!

标签: arrays listview xamarin.forms data-binding


【解决方案1】:

你需要修改xaml如下:-

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding runningLateOption}" ItemSelected="runningLateSelectedItem">
     <ListView.ItemTemplate>
        <DataTemplate>
          <Label x:Name="listItems" Text="{Binding .}" HorizontalTextAlignment="Center"/>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

如果您遇到更多困难,请告诉我。

【讨论】:

  • 你修改了ItemSource ItemsSource="{Binding runningLateOption}" 吗?
  • 您需要在 ViewModel 中实现 INotifyPropertyChanged,您可以通过使用 MVVMHelpers Nuget 包来实现,并将其用作 ViewModel 中声明的属性
【解决方案2】:

ListViewCollectionViewItemsSource类型应该是实现IEnumerable接口的列表。 p>

所以我们通常将其设置为ObservableCollection&lt;T&gt;List&lt;T&gt;

在你的情况下,你可以设置 ListView 的 ItemsSource 像

public ObservableCollection<string> RunningLateOptions {get; set;}

ObservableCollection 实现了接口INotifyPropertyChanged。所以你不需要再实现它了。

关于 ListView 的更多详细信息,您可以参考https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
   <ListView.ItemTemplate>
       <DataTemplate>
           <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
              <Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
           </StackLayout>
       </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

【讨论】:

  • 你可以分享一个样本,这样我就可以在我身边进行测试。
  • 我提供了上面的 Listview 和 View Model。我没有使用除此之外。只是该数组没有绑定到列表视图。
  • 在您的设备上提供效果的屏幕截图。
【解决方案3】:

我在 CS 文件中给出了字符串数组,并使用 itemsource 绑定到 listview。 我在这里没有使用任何 View 模型。

CS 代码:

string[] runningLateOptions = { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours"  };
RunningLateOptions.ItemsSource = runningLateOptions;

XAML 代码:

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding Items}" ItemSelected="runningLateSelectedItem">
   <ListView.ItemTemplate>
     <DataTemplate>
        <ViewCell>
          <Label BackgroundColor="Transparent" x:Name="listItems" Text="{Binding}" TextColor="Black" HorizontalOptions="Center"></Label>
         </ViewCell>
     </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

TextCell 未提供 Horizo​​ntalTextAlignment 属性。这就是我使用视图单元格作为标签的原因。

感谢您帮助我。 Click here to see the output

【讨论】:

  • 别忘了采纳你的答案,这将帮助更多的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2012-04-23
  • 2012-05-05
  • 1970-01-01
相关资源
最近更新 更多