WPF 绑定 DataGridComboBoxColumn 的 SelectedItemBinding

WPF binding SelectedItemBinding of a DataGridComboBoxColumn

我有一个学生class定义了一个学生的属性如下

 public class Student
    {
        public string Department { get; set; }

    public string Level { get; set; }

    public Gender Gender { get; set; }

    public string Nationality { get; set; }

}

我有另一个 class,其中包含如下所示声明的集合中的国家/地区列表

public class Countries
    {
 public static ObservableCollection<string> Nationalities { get; } = new ObservableCollection<string>()
        {
            "Afghanistan",
            "Albania",
            "Algeria",
            "Andorra",
            "Angola",
            "Anguilla",
            "Antigua & Barbuda",
            "Argentina",
            "Armenia",
            "Australia",
            "Austria",
            "Azerbaijan",
            "Bahamas",
            "Bahrain",
            "Bangladesh",
            "Barbados",
            "Belarus",
            "Belgium",
            "Belize",
            "Benin",
            "Bermuda",
            "Bhutan",
            "Bolivia",
            "Bosnia & Herzegovina",
            "Botswana",
            "Brazil",
}

在我的视图模型中,我声明了一个初始化为国家列表的可观察集合。这是在视图模型的构造函数中完成的,如下所示

public class ViewModel : BaseViewModel
    {
     public ObservableCollection<string> Nationalities { get; private set; }
         public ViewModel()
        {

            // Set the nationality list
            Nationalities = Countries.Nationalities;

        }
}

现在我已经像这样创建了 DataGridComboBoxColumn,

 <DataGrid.Columns>
                        <DataGridComboBoxColumn Width="Auto"
                                                    SelectedItemBinding="{Binding Path=Student.Nationality}"
                                                    Header="Nationality" 
                                                    x:Name="NationalityComboBox2">
                            <DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Nationalities, ElementName=StudentListGrid}" />
                                </Style>
                            </DataGridComboBoxColumn.EditingElementStyle>
                        </DataGridComboBoxColumn>
                    </DataGrid.Columns>

当我 运行 我的应用程序时,DataGridComboBoxColumn 有所有国家,但是当我 select 一个特定国家时,它没有 displayed.I 在网上看到类似的问题,但我还没有看到任何适用于我的情况的答案。有什么建议吗?

我认为您需要在 Student

上实现 INotifyPropertyChanged 接口
public class Student : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public string Department { get; set; }

    public string Level { get; set; }

    public Gender Gender { get; set; }

    private string _nationality;
    public string Nationality 
    { 
        get
        {
            return _nationality;
        }  
        set
        {
            if(value != _nationality)
            {
                _nationality = value;
                OnPropertyChanged(nameof(Nationality));
            }
         } 
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

假设 DataGridItemsSource 是一个 IEnumerable<Student> 你应该绑定到 Nationality 而不是 Student.Nationality:

<DataGridComboBoxColumn Width="Auto"
                        SelectedItemBinding="{Binding Nationality}"
                        Header="Nationality" 
                        x:Name="NationalityComboBox2">
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Nationalities, ElementName=StudentListGrid}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

您可以使用 DataGridTemplateColumn 而不是使用 DataGridComboBoxColumn 来实现相同的目的。您可能需要编写一些代码来保持文本框和组合框的样式。

 <DataGridTemplateColumn  Header="Nationality" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Nationality}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox SelectedItem="{Binding Nationality}" ItemsSource="{Binding Path=DataContext.Nationalities, ElementName=StudentListGrid}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>