【问题标题】:Xamarin Forms Using multiple lists in ViewModelXamarin 表单在 ViewModel 中使用多个列表
【发布时间】:2020-01-31 06:57:24
【问题描述】:

我有问题。我创建了一个带有 ViewModel 的 CollectionView,在该 ViewModel 中我有 2 个不同的列表。现在我知道如何显示来自 1 个列表的数据,但现在我想在数据模板中添加一个带有第二个列表数据的选择器。这是xaml代码:

<CollectionView ItemsSource="{Binding imageList}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>

            <Picker Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Title="Formaat" ItemsSource="{Binding secondList}" HorizontalOptions="FillAndExpand" />

            <Label Grid.Row="0" Grid.Column="4" VerticalTextAlignment="Center" VerticalOptions="Center"  
                    HorizontalOptions="Center" Text="{Binding Price, StringFormat='€ {0:F2}'}" TextColor="Black"
                    FontSize="18" />
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

所以价格确实从 imageList 中显示出来,但 Picker 是空的。在 Picker 中,我想要来自 secondList 的元素 Name

我该如何解决这个问题?

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    问题是CollectionView 元素的DataTemplate 中的BindingContext 是来自imageList 的项目。因此绑定将不起作用,因为我假设您的 secondList 不是您在 imageList 中使用的元素模型的一部分,而是用于页面的整个视图模型的一部分。

    我建议使用RelativeBinding。我不知道您的模型名称,但您的 Picker 绑定可能如下所示:

    <Picker ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type local:YourViewModelTypeName}}, Path=secondList}" />
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2018-03-13
      • 2020-12-21
      • 1970-01-01
      • 2021-08-06
      • 2021-10-20
      相关资源
      最近更新 更多