Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Picker not updating selected item from MVVM

$
0
0

I have a form like below with picker control. Its source is in MVVM's observableCollection. Depending if it's existent Process or new one, _this object is created from scratch or set to object passed in constructor.
All works good, meaning picker get populated and _this contains appropriate object. The thing is, though, if _this is existent object with specified Type property, appropriate item in picker won't get selected automatically when form is loaded. I mean I see picker's title instead of item that's supposed to be selected. I'm wondering what might be wrong. Any ideas?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestZXing.ProcessPage">
    <ContentPage.Content>
        <StackLayout VerticalOptions="Center">
            <Picker
                x:Name="cmbActionTypes"
                HorizontalOptions="Center" 
                Title="Wybierz typ zgłoszenia"
                ItemsSource="{Binding ActionTypes}"
                ItemDisplayBinding="{Binding Name}"
                SelectedItem="{Binding Type, Mode=TwoWay}"
                />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

There's a MVVM model behind it:

public class ProcessPageViewModel: INotifyPropertyChanged
    {
        public ObservableCollection<ActionType> ActionTypes { get; set; }
        private Process _this { get; set; }

        public ProcessPageViewModel()
        {
            _this = new Process();
            IsSaved = false;
            Initialize();
        }

        public ProcessPageViewModel(Process Process)
        {
            _this = Process;
            Initialize();
        }

        private async void Initialize()
        {
            try
            {
                ActionTypes = new ObservableCollection<ActionType>();
                ActionTypesKeeper keeper = new ActionTypesKeeper();
                await keeper.Reload();
                foreach (ActionType at in keeper.Items)
                {
                    ActionTypes.Add(at);
                }
            }catch(Exception ex)
            {
                throw;
            }

        }

        private ActionType _type { get; set; }

        public ActionType Type
        {
            get
            {
                if(ActionTypes.Where(at => at.ActionTypeId == _this.ActionTypeId).Any())
                {
                    _type = ActionTypes.Where(at => at.ActionTypeId == _this.ActionTypeId).FirstOrDefault();
                }
                return _type;
            }
            set
            {
                if (_type != value)
                {
                    _type = value;
                    _this.ActionTypeId = _type.ActionTypeId;
                    _this.ActionTypeName = _type.Name;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>