Picker ItemSource值未绑定到XAML ListView中 [英] Picker ItemSource values not binding in a XAML ListView

查看:54
本文介绍了Picker ItemSource值未绑定到XAML ListView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能提出建议,因为我已经尝试了所有方法来使用代码隐藏和MVVM将Listview中的选择器绑定到我的商品源值.选择器绑定在 ListView 之外工作,但不在内部 - 这是开箱即用还是不可能的?

Hoping you can advise as I've tried all methods to bind a picker in a Listview to my item source values using both code-behind and MVVM. The picker binding works outside of the ListView, but not inside - is this out of the box or just not possible?

请在下面查看我的列表!

Please see my list below!

代码

                    <ListView
                            x:Name="TypesList"                
                            HeightRequest="53"
                            BackgroundColor="White"
                            IsGroupingEnabled="True"
                            IsPullToRefreshEnabled="false"
                            ItemsSource="{Binding Items}"                
                            IsRefreshing="{Binding IsBusy}"
                            RefreshCommand="{Binding LoadTypesCommand}"
                            SeparatorVisibility="None"  
                            >
                      
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell Height="63">
                                    <StackLayout Orientation="Horizontal"  VerticalOptions="Center">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="150"/>
                                            </Grid.RowDefinitions>

                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="250"/>
                                                <ColumnDefinition Width="60"/>
                                                <ColumnDefinition Width="70"/>
                                            </Grid.ColumnDefinitions>
                                            <!-- -->
                                            <Label Grid.Column="0"
                                                   Grid.Row="0"
                                                   VerticalOptions="Center"
                                                   FontAttributes="None"                                       
                                                   Text="{Binding .Name, Mode=TwoWay} "
                                                   TextColor="Black"
                                                   VerticalTextAlignment="Center"
                                                   Margin="0,15,0,110"
                                                   BackgroundColor="White" 
                                                   Padding="20,10,0,10"
                                                   FontFamily="Hiragino Sans"
                                                   FontSize="14"
                                                   HeightRequest="53"
                                                   x:Name="userLabel"                                           
                                                  />
                                           
                                            <Entry Grid.Column="1"
                                                   Grid.Row="0"
                                                   Text="{Binding .Amount, Mode=TwoWay}"
                                                   TextColor="Black"
                                                   VerticalOptions="EndAndExpand"
                                                   Margin="0,15,20,110" 
                                                   BackgroundColor="White"                                    
                                                   FontFamily="Hiragino Sans"
                                                   FontSize="14"
                                                   HeightRequest="40"
                                                   WidthRequest="55"
                                                   x:Name="userEntry"
                                            />
                                            <Picker Grid.Column="2"
                                                    Grid.Row="0"
                                                    x:Name="mPicker"
                                                    ItemsSource="{Binding UserOptions}"
                                               
                                            />
                                        </Grid>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate> 
                        </ListView.ItemTemplate>

代码

后面的代码

代码mPicker.ItemsSource = ViewModel.UserOptions;代码

code mPicker.ItemsSource = ViewModel.UserOptions; code

该模型分配了UserOptions,这适用于列表视图之外的选择器,因此我不认为该模型有任何问题或需要发布此代码.

The model allocates UserOptions and this works for a picker outside the listview so I don't believe there is anything wrong with the model or this code needs to be posted.

任何帮助表示赞赏!

推荐答案

列表视图具有其自己的绑定上下文.因此,您需要获取视图模型的绑定上下文,然后像这样绑定到选择器

List view has its own binding context. So you need to get binding context of view model and then bind to picker like this

像这样设置视图名称

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Name="viewName">


<Picker Grid.Column="2"
        Grid.Row="0"
        x:Name="mPicker"
        ItemsSource="{Binding BindingContext.UserOptions, Source={x:Reference viewName}}"
                                               
/>

这篇关于Picker ItemSource值未绑定到XAML ListView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆