从组合框获取绑定的选定项目

时间:2020-07-17 05:57:11

标签: c# wpf

我的组合return nil, nil已绑定到cbStudents

StudentsList

我想获得 <ComboBox Name="cbStudents" ItemsSource="{Binding Path=Students}" SelectedItem="{Binding Path=SelectedStudent, Mode=TwoWay}" DisplayMemberPath="Name"/> public class Student { private string _name; public string Name { get { return _name; } set { _name = value; } } private int _age; public int Age { get { return _age; } set { _age= value; } } }

我尝试过-

SelectedItem

结果: 我得到 private ObservableCollection<Language> _students; public ObservableCollection<Language> Students { get { return _students; } set { _students= value; } } private string _selectedStudent; public string SelectedStudent { get { return _selectedStudent; } set { _selectedStudent= value; } } private void btnGetOutput(object sender, RoutedEventArgs e) { MessageBox.Show("Your selected item is: " + SelectedStudent); } 的输出。没有错误代码,什么都没有。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:2)

您似乎想按其姓名(即string)选择一名学生。

然后应使用SelectedItemSelectedValue代替SelectValuePath

<ComboBox
    ItemsSource="{Binding Students}"
    DisplayMemberPath="Name"
    SelectedValuePath="Name"
    SelectedValue="{Binding SelectedStudent}" />

视图模型当然必须实现INotifyPropertyChanged,并且应该将Students属性声明为只读:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Student> Students { get; }
        = new ObservableCollection<Student>();

    private string selectedStudent;

    public string SelectedStudent
    {
        get { return selectedStudent; }
        set
        {
            selectedStudent = value;
            PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs(nameof(SelectedStudent)));
        }
    }
}

答案 1 :(得分:1)

首先,您已经定义了ObservableCollection<Language> Students,我认为应该改为ObservableCollection<Student> Students

然后,您必须绑定到该集合,而不是绑定到示例中不存在的StudentList(也许您没有提供完整的代码?)。

然后,您必须在Students.Add(...)某处的列表中添加项目。

然后,如果组合框中的项目为Student类型,则绑定到SelectedItem的属性也必须为Student类型,而不是string

最后但并非最不重要的一点:您必须将类与定义到视图的所有这些字段绑定在一起,因此必须编写:view.DataContext = objectWithData;,其中view是您的视图,objectWithData是对象定义了这些字段。

答案 2 :(得分:1)

您可以实现INotifyPropertyChanged接口。如下例所示:

public class Data : INotifyPropertyChanged
{
    // boiler-plate
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetValue<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }
    
    private ObservableCollection<Language> _students;

    public ObservableCollection<Language> Students
    {
       get { return _students; }
       set { _students= value; }
    }

    private Language _selectedStudent;

    public Language SelectedStudent
    {
       get { return _selectedStudent; }
       set { SetValue(_selectedStudent, value, "SelectedStudent"); }
    }
}
相关问题