Search code examples
wpfaccessibility

Accessibility Narrator reads the class name of the selected Item in the ComboBox instead of the DisplayMemberPath value


Accessibility Narrator reads the class name of the selected Item in the ComboBox instead of the DisplayMemberPath value

public class Student
{
    public int Id { get; set; }
    public string? Name { get; set; }
}
public class MainViewModel : ViewModelBase
{
    private ObservableCollection<Student> _students;
    private Student _selectedStudents;
    public ObservableCollection<Student> Students
    {
        get
        {
            if (_students == null)
            {
                _students = new ObservableCollection<Student>();
                _students.Add(new Student() { Id = 1, Name = "A" });
                _students.Add(new Student() { Id = 2, Name = "B" });
                _students.Add(new Student() { Id = 3, Name = "C" });
            }
            return _students;
        }
    }

    public Student SelectedStudents
    {
        get
        {
            return _selectedStudents;
        }
        set
        {
            _selectedStudents = value;
            RaisePropertyChanged(nameof(SelectedStudents));
        }
    }
}

XAML

<ComboBox
        Height="30"
        AutomationProperties.Name="Students"
        DisplayMemberPath="Name"
        ItemsSource="{Binding Path=Students}" 
        SelectedItem="{Binding SelectedStudents}"/>

When I select the first item of the ComboBox Narrator reads the Student class name instead of the name value "A"

Any suggestions?


Solution

  • I fixed this by overriding ToString in the model class:

    public class Student
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public override string ToString()
        {
            return Name ?? "Student";
        }
    }