Picker - Binding Context

capitolcapitol ✭✭Member ✭✭

Currently have a picker that won't display the list of items that are bound to it.

I have a ListView that is using the exact same binding, and it displays the list no problems at all.

Code below:

        <StackLayout IsVisible="{Binding IsDropdownControl}">
          <Label Text="Task Field:"/>
          <Picker Title="Select a Value:"
                        ItemsSource="{Binding FieldItems}" BindingContext="{Binding SelectedField}"
                        ItemDisplayBinding="{Binding Name}"
                  SelectedItem="{Binding SelectedFieldItem}"
                  HorizontalOptions="FillAndExpand">
          </Picker>
        </StackLayout>


        <StackLayout IsVisible="{Binding IsCheckBoxControl}">
          <Label Text="Select Options:"/>
          <ListView ItemsSource="{Binding FieldItems}" BindingContext="{Binding SelectedField}"
                HasUnevenRows="True"
                SeparatorColor="Gray"
                VerticalOptions="FillAndExpand"
                HorizontalOptions="FillAndExpand">
            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal">
                    <Label Text="{Binding Name}" TextColor="Black" FontSize="Small" 
                             HorizontalOptions="StartAndExpand"/>
                    <Switch HorizontalOptions="End" 
                              IsToggled="{Binding Value}"/>
                  </StackLayout>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>
        </StackLayout>

Answers

  • JarvanJarvan Xamurai Member, Xamarin Team Xamurai
    edited April 12

    @capitol I've reproduced the issue and it works fine. You can refer to it.
    page.xaml.cs

    public partial class MainPage : ContentPage
    {
        ObservableCollection<Model_> list = new ObservableCollection<Model_>();
        public MainPage()
        {
            InitializeComponent();
    
            list.Add(new Model_ { Name = "name1", Enabled = true });
            list.Add(new Model_ { Name = "name2", Enabled = false });
            list.Add(new Model_ { Name = "name3", Enabled = true });
            list.Add(new Model_ { Name = "name4", Enabled = false });
            list.Add(new Model_ { Name = "name5", Enabled = true });
    
            listview.ItemsSource = list;
    
            List<string> list_picker = new List<string>();
            list_picker.Add("name1");
            list_picker.Add("name2");
            list_picker.Add("name3");
            list_picker.Add("name4");
            picker.ItemsSource = list;
        }
    }
    
    public class Model_
    {
        public string Name { get; set; }
    
        public bool Enabled { get; set; }
    }
    

    page.xaml

    <StackLayout>
        <StackLayout>
            <Label Text="_picker" />
            <Picker x:Name="picker"
                        Title="picker_select"
                        ItemDisplayBinding="{Binding Name}"
                        SelectedItem="{Binding Name}"
                        />
        </StackLayout>
        <StackLayout>
            <ListView x:Name="listview">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding Name}" TextColor="Black" FontSize="Small" 
                                 HorizontalOptions="StartAndExpand"/>
                                <Switch HorizontalOptions="End" 
                                  IsToggled="{Binding Enabled}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </StackLayout>
    

  • capitolcapitol ✭✭ Member ✭✭

    @yelinzh Thanks for the reply! :)

    But I'm using MVVM so there's nothing in the codebehind and nothing in the VM that corresponds to the View.

    Also, I'm binding to a list of objects within a property. This seems to work for a listview, but not a picker...?

  • JarvanJarvan Xamurai Member, Xamarin Team Xamurai

    Would you mind sharing a basic demo? It'll be helpful to reproduce the issue and find a solution.

  • capitolcapitol ✭✭ Member ✭✭
    edited April 18

    @yelinzh said:
    Would you mind sharing a basic demo? It'll be helpful to reproduce the issue and find a solution.

    Demo Attached.

    It seems to work no problem in the demo but wouldn't in the original context.

    Any idea what could have created this behavior?

    Maybe using complex objects as an itemsource?

  • JarvanJarvan Xamurai Member, Xamarin Team Xamurai

    Is this you want? I deployed your demo and the picker could display list's items.

Sign In or Register to comment.