Forum Xamarin.Forms

Picker SelectedItem Binding does not Update

Hello together

I have the problem that the binding which I set for the selecteditem of the picker will not change when the variable behind has changed.

xml of the picker:
<Picker ItemsSource="{Binding TeamListe}" SelectedItem="{Binding SelectedItem}" />

The code of the viewmodel

static string _SelectedItem;
public string SelectedItem
{
      get { return _SelectedItem; }
      set
      {
            _SelectedItem = value;
           OnPropertyChanged("SelectedItem");
       }
 }

When I set the value on appearing it works.
But when I ty to change the selecteditem as an example when I clicked on a button it does not work.

Other bindings like the ItemSource of a Listview in the same project do work.

What do I miss?
Thanks for your help.
Regards

Best Answer

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @xama12 said:
    How do I access the current?

    ((MainPageViewModel) BindingContext).SelectedItem = "Two";

Answers

  • JohnHardmanJohnHardman GBUniversity admin
    edited November 2020

    @xama12

    Make the BindingMode for SelectedItem TwoWay

  • xama12xama12 Member ✭✭

    @JohnHardman

    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    Tried it, does not work.

  • JohnHardmanJohnHardman GBUniversity admin

    @xama12 said:
    @JohnHardman

    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    Tried it, does not work.

    Show the full XAML and C#

  • xama12xama12 Member ✭✭

    Full code:

    MainPage.xml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="example_.MainPage">
        <StackLayout>
            <Label Text="Test project for showing the problem with the picker"/>
            <Picker ItemsSource="{Binding TeamListe}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
            <Button Text="Test: Change picker selecteditem" Clicked="Button_Clicked"/>
        </StackLayout>
    </ContentPage>
    

    MainPage.cs

    public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
                BindingContext = new MainPageViewModel();
            }
    
            private void Button_Clicked(object sender, EventArgs e)
            {
                var viewModel = new MainPageViewModel();
                viewModel.SelectedItem = "Two";
            }
        }
    

    MainPageViewModel.cs

    class MainPageViewModel : BaseViewModel
        {
            public MainPageViewModel()
            {
                if (_TeamListe == null)
                {
                    TeamListe = new ObservableCollection<string>() { "One", "Two", "Three" };
                    SelectedItem = "One";
                }
            }
    
            static ObservableCollection<string> _TeamListe;
            public ObservableCollection<string> TeamListe
            {
                get { return _TeamListe; }
                set
                {
                    if (_TeamListe == null)
                    {
                        _TeamListe = value;
                    }
                    else
                    {
                        _TeamListe.Clear();
                        foreach (var item in value)
                        {
                            _TeamListe.Add(item);
                        }
                    }
                    OnPropertyChanged();
                }
            }
    
            static string _SelectedItem;
            public string SelectedItem
            {
                get { return _SelectedItem; }
                set
                {
                    _SelectedItem = value;
                    OnPropertyChanged("SelectedItem");
                }
            }
        }
    

    BaseviewModel.cs

    class BaseViewModel : INotifyPropertyChanged
        {
            protected BaseViewModel()
            {
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(
            [CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs(propertyName));
            }
        }
    
  • JohnHardmanJohnHardman GBUniversity admin

    @xama12 said:

            public MainPage()
            {
                InitializeComponent();
                BindingContext = new MainPageViewModel();
            }
    
            private void Button_Clicked(object sender, EventArgs e)
            {
                var viewModel = new MainPageViewModel();
                viewModel.SelectedItem = "Two";
            }
        }
    

    In your Clicked handler, you are instantiating a new view model instead of setting the SelectedItem of your existing view model.

  • xama12xama12 Member ✭✭

    How do I access the current?

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @xama12 said:
    How do I access the current?

    ((MainPageViewModel) BindingContext).SelectedItem = "Two";

Sign In or Register to comment.