UWP Combobox doesn't return a selected value in MVVM

Viewed 25

I struggle getting a combobox to work in a MVVM UWP app. I have it working in the code behind. That was not too difficult but now I have a ViewModel that gives problems. As usual I spend a day or so on the internet to find a similar question and solution but nothing there. Whatever I try results in a Null Exception and I can't figure out why I cannot simply select a value from the Combobox and transfer it to my code.

This is how I did it in the code behind and where "(int)golfdropdown.SelectedValue" did the job.

   private void WireUpSpelerDropDown()
    {
        string sql = "select * from Golfclub ";
        var golfclublijst = SqliteDataAccess.LoadData<GolfclubModel>(sql, new Dictionary<string, object>());

        golfclublijst.ForEach(x => golfclub.Add(x));

        golfclubDropDown.ItemsSource = golfclub;
        golfclubDropDown.DisplayMemberPath = "Naam";
        golfclubDropDown.SelectedValuePath = "Id";
    }

enter code here

This is a piece of my mvvm code:

public class SpelerViewModel : INotifyPropertyChanged
{
    public ObservableCollection<SpelerModel> Spelers = new ObservableCollection<SpelerModel>();
    public ObservableCollection<GolfclubModel> Golfclubs = new ObservableCollection<GolfclubModel>();
    
    public bool NewRecordClearFields = false;

    public int GolfclubId { get; set; } = 3;

    public SpelerModel NewSpeler { get; set; }
    public SpelerViewModel()
    {
        NewSpeler = new SpelerModel();
        GetData();
        WireUpSpelerDropDown();
    }
    private void WireUpSpelerDropDown()
    {
       
        string sql = "select * from Golfclub ";
        var golfclublijst = SqliteDataAccess.LoadData<GolfclubModel>(sql, new Dictionary<string, object>());

        golfclublijst.ForEach(x => Golfclubs.Add(x));
    }

XAML:

 <ComboBox x:Name="golfclubDropDown" 
                      ItemsSource="{x:Bind Viewmodel.Golfclubs, Mode=OneWay}"                          
                      SelectedValue ="{x:Bind Viewmodel.GolfclubId, Mode=TwoWay}"
                      DisplayMemberPath="Naam"
                      SelectedValuePath="Id"
                      Header="Homeclub"
                      Margin=" 8"
                      MinWidth="200">
            </ComboBox>
0 Answers
Related