Home > database >  Bind SelectedItem to Combo Box
Bind SelectedItem to Combo Box

Time:09-25

I'm trying to bind the SelectedStudent property to Combo1.

public List<string> StudentsList { get; set; }
public String SelectedStudent { get; set; }

XAML:

<ComboBox Name="Combo1"
    ItemsSource="{Binding StudentsList}"
    SelectedItem="{Binding SelectedStudent, Mode=TwoWay}"/>

Problem: Every time I select an item from the Combo1, SelectedStudent remains null/ empty.

I have already gone through many similar answers including this but none helped: Binding ComboBox SelectedItem using MVVM

CodePudding user response:

try this

    ComboBox Name="Combo1"
          ItemsSource="{Binding Path=StudentsList}"
          DisplayMemberPath="Name"
          SelectedValuePath="Name"
          SelectedValue="{Binding Path=SelectedStudent, UpdateSourceTrigger=PropertyChanged }" />

classes

public List<StudentListEntry> StudentsList { get; set; }
public string SelectedStudent { get; set; }

public class StudentListEntry
{
   public string Name { get; set; }
}
```
  • Related