0

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.

  • 1
    Besides that you would usually not bind SelectedItem and SelectedIndex at the same time, what is the initial value of the Position view model property? It certainly resets the selection set by SelectedIndex. What's the reason to bind both? – Clemens May 14 '20 at 10:57
  • 1
    In order to select a ComboBox item by the value of the Position property of the employee item, just set `SelectedValuePath="Position"` and bind `SelectedValue="{Binding Position}"` instead of SelectedItem and SelectedIndex. – Clemens May 14 '20 at 11:02
  • @Clemens it didn't work with SelectedItem, so i tried to "brute force" it with SelectedIndex. ```Position``` depends on what item is clicked, then value is taken and sent to ```ComboBox``` but it believes its null. –  May 14 '20 at 11:03
  • 1
    And as note, `Array.IndexOf(employee.Positions.ToArray(), employee.Position)` is horrible. You don't need it at all, but next time write `employee.Positions.IndexOf(employee.Position)`. – Clemens May 14 '20 at 11:04
  • @Clemens using ```SelectedValuePath``` and switching to ```SelectedValue``` did the trick. Thanks for help, for some reason online guides skip some things which creates problems, or i'm just using some advanced things without knowing. –  May 14 '20 at 11:07

0 Answers0