I have a window where i can edit specific object based on what is clicked. It opens as it should, filling all TextBoxes, and even DatePicker, but ComboBox starts as empty. I have found a few similar issues here yet their offered solutions don't change anything.
XAML code:
<ComboBox Name="Position"
Grid.Row="5"
SelectedIndex="{Binding SelectedIndex}"
SelectedItem="{Binding Position}"
ItemsSource="{Binding Positions}"
DisplayMemberPath="Position"
Style="{StaticResource MaterialDesignComboBox}"
IsEnabled="True"
IsEditable="False"
IsReadOnly="True"
Margin="15,1,15,1"
FontSize="12"/>
.cs
WindowAddEditEmployeesViewModel employee = new WindowAddEditEmployeesViewModel();
public WindowEditEmployees(int id, string firstName, string lastName, string position, string email, string phoneNumber, string address, string postalCode, string city, string login, string password, Nullable<DateTime> dateOfBirth)
{
InitializeComponent();
employee.Positions = new ObservableCollection<PositionsViewModel>(positionsMethods.GetAll());
employee.idEmployee = id;
employee.FirstName = firstName;
employee.LastName = lastName;
employee.Position = position;
employee.Email = email;
employee.PhoneNumber = phoneNumber;
employee.Address = address;
employee.PostalCode = postalCode;
employee.City = city;
employee.Login = login;
employee.Password = password;
employee.DateOfBirth = dateOfBirth;
employee.SelectedIndex = Array.IndexOf(employee.Positions.ToArray(), employee.Position);
DataContext = employee;
}
ViewModel:
public class WindowAddEditEmployeesViewModel : EmployeesViewModel, INotifyPropertyChanged
{
public ObservableCollection<PositionsViewModel> Positions { get; set; }
public int SelectedIndex { get; set; }
new public event PropertyChangedEventHandler PropertyChanged;
}
Base view model contains things like FirstName, LastName, etc.
So as you can see from the code it should work, both SelectedIndex
and SelectedItem
are properly set, yet it refuses to work.