Set SelectedItem for Picker within a ListView's DataTemplate ViewCell

ksumarineksumarine USMember ✭✭

Hello, I have a custom ContentView (where Group is being set through a custom property) which has a ListView of People within the Group. Each Cell needs to have a picker that's populated from a list of positions. I need the picker in each cell to default to each person's position and also update when it's changed.

in App.xaml.cs (for now until I get a web service to return this)

public static ObservableCollection<Models.Position> Positions = new ObservableCollection<Models.Position> {
    new Models.Position {PosId = 1, Abbr = "pos 1"},
    new Models.Position {PosId = 2, Abbr = "pos 2"},
    new Models.Position {PosId = 3, Abbr = "pos 3"},
    new Models.Position {PosId = 4, Abbr = "pos 4"},
    new Models.Position {PosId = 5, Abbr = "pos 5"},
    new Models.Position {PosId = 6, Abbr = "pos 6"},
    new Models.Position {PosId = 7, Abbr = "pos 7"},
    new Models.Position {PosId = 8, Abbr = "pos 8"},
    new Models.Position {PosId = 9, Abbr = "pos 9"}
};

xaml...

<?xml version="1.0" encoding="UTF-8"?>
<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="App.Views.GridListView"
    x:Name="GroupList">
    <ContentView.Content>
        <StackLayout
            Orientation="Vertical"
            VerticalOptions="Fill"
            HorizontalOptions="Fill"
            Padding="5">
            <Label
                HorizontalOptions="StartAndExpand"
                VerticalOptions="Start"
                Text="{Binding Group.GroupName}" />
            <ListView
                ItemsSource="{Binding Group.People}"
                HasUnevenRows="true"
                BackgroundColor="Transparent"
                x:Name="PersonList">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout
                                Orientation="Horizontal"
                                Padding="10">
                                <Label
                                    Text="{Binding FullName}" />
                                <Picker
                                    ItemsSource="{Binding Path=BindingContext.Positions, Source={x:Reference GroupList}}"
                                    ItemDisplayBinding="{Binding Abbr}"
                                    SelectedItem="{Binding Position, Mode=TwoWay, Source={x:Reference PersonList}}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentView.Content>
</ContentView>

View Model...

namespace App.ViewModels {
    public class GridListViewModel:BaseViewModel {
        private Models.Group _Group;
        private RangeObservableCollection<Models.People> _People;
        private Models.Position _selectedPosition;

        public Models.Group Group {
            get { return _Group; }
            set { SetProperty(ref _Group, value); }
        }
        public RangeObservableCollection<Models.Position> Positions {
            get { return App.Positions; }
        }
    }
}

Models...

public partial class Group {
    public Group() {
        People = new List<Person> { };
    }
    public virtual Guid GroupId { get; set; }
    public virtual string GroupName { get; set; }
    public virtual List<Person> People { get; set; }
}
public partial class Person {
    public virtual Guid PersonId { get; set; }
    public virtual Guid GroupId { get; set; }
    public virtual Position Position { get; set; }
    public virtual string FullName { get; set; }
}
public partial class Position {
    public virtual int PosId { get; set; }
    public virtual string Abbr { get; set; }
}

Everything is working except default selecting the position based on the person's ViewCell. Can anyone help out?

I appreciate it!

Best Answer

Answers

Sign In or Register to comment.