我有一个窗口,我可以在其中基于单击的内容来编辑特定对象。它会按应打开的方式填充所有TextBox,甚至是DatePicker,但ComboBox开头为空。我在这里发现了一些类似的问题,但是他们提供的解决方案没有任何改变。
XAML代码:
<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;
}
基本视图模型包含FirstName,LastName等内容。
因此,从代码中可以看到它应该正常工作,SelectedIndex
和SelectedItem
均已正确设置,但它拒绝工作。