繁体   English   中英

从 Combobox 获取绑定的选中项

[英]Get Bound Selected Item from Combobox

我的组合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);
    }

结果:我得到Null output。没有错误代码,什么都没有。 任何帮助表示赞赏。

看起来你想要 select 一个学生的名字,即一个string

而不是SelectedItem ,你应该然后使用SelectedValueSelectValuePath

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

视图 model 当然必须实现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)));
        }
    }
}

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

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

然后,您必须将项目添加到该列表的某处, Students.Add(...)

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

最后但同样重要的是:您必须将 class 与定义到您的视图的所有这些字段绑定,因此您必须编写: view.DataContext = objectWithData; ,其中view是您的视图, objectWithData是定义了这些字段的 object。

您可以实现 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"); }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM