C# 组合框空项

C# 组合框空项,c#,wpf,combobox,C#,Wpf,Combobox,我正在尝试将组合框与空项绑定。为此,我使用了我在许多问题中读到的复合集合 这是目前的工作。然而,使用复合集合会弄乱我的分组。要么我在组合框中得到空白,要么得到分组。我希望两者都有空 下面是我一直在使用的示例代码: Xaml viewmodel和支持类: public class Course { public int CourseID { get; set; } public string CourseName { get; set; } public string

我正在尝试将组合框与空项绑定。为此,我使用了我在许多问题中读到的
复合集合

这是目前的工作。然而,使用复合集合会弄乱我的分组。要么我在组合框中得到空白,要么得到分组。我希望两者都有空

下面是我一直在使用的示例代码:

Xaml


viewmodel和支持类:

public class Course
{
    public int CourseID { get; set; }
    public string CourseName { get; set; }
    public string Major { get; set; }
}

public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel() 
    {
        _courses = new List<Course>();
        _courses.Add(new Course { CourseID = 1, CourseName = "Math 107", Major = "Math" });
        _courses.Add(new Course { CourseID = 1, CourseName = "Biology 110", Major = "Biology" });
        _courses.Add(new Course { CourseID = 1, CourseName = "Chemistry 123", Major = "Chemistry" });
        _courses.Add(new Course { CourseID = 1, CourseName = "Spanish 112", Major = "Biology" });
        _courses.Add(new Course { CourseID = 1, CourseName = "Molecular 114", Major = "Biology" });
    }

    private List<Course> _courses;

    public List<Course> Courses
    {
        get { return _courses; }
        set { _courses = value; OnPropertyChanged(); }
    }

    private Course _selectedCourse;

    public Course SelectedCourse
    {
        get { return _selectedCourse; }
        set { _selectedCourse = value; OnPropertyChanged(); }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

public class CourseConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Course)
            return value;
        else
            return null;
    }
}
公共课
{
public int CourseID{get;set;}
公共字符串CourseName{get;set;}
公共字符串主键{get;set;}
}
公共类MainWindowViewModel:INotifyPropertyChanged
{
公共主窗口视图模型()
{
_课程=新列表();
_添加(新课程{CourseID=1,CourseName=“Math 107”,Major=“Math”});
_添加(新课程{CourseID=1,CourseName=“Biology 110”,Major=“Biology”});
_添加(新课程{CourseID=1,CourseName=“Chemistry 123”,Major=“Chemistry”});
_添加(新课程{CourseID=1,CourseName=“西班牙语112”,Major=“生物学”});
_添加(新课程{CourseID=1,CourseName=“Molecular 114”,Major=“Biology”});
}
私人名单课程;
公开名单课程
{
获取{return\u courses;}
设置{u courses=value;OnPropertyChanged();}
}
私人课程(你选择的课程);;
公共课程选修课程
{
获取{return\u selectedCourse;}
设置{u selectedCourse=value;OnPropertyChanged();}
}
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共类课程转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回值;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
如果(值为课程)
返回值;
其他的
返回null;
}
}
我是否缺少一些东西使它能够在分组和不分组的情况下工作。为了删除分组,我删除了内联ItemSource声明并使用注释代码。这显示了WPF中的空白.

,我们使用的是数据项,而不是UI元素。我们声明
DataTemplate
s来定义数据项在任何UI容器中的外观。使用这种方法,我们让框架负责显示UI,并专注于数据。因此,要在WPF中显示一个空的UI元素,只需将一个空数据项添加到集合中,并让
DataTemplate
执行其工作:

_courses.Add(new Course());

这将被简单地呈现为一个空项,因为它没有要在任何数据绑定控件中显示的数据。因此,请尝试在
组合框.ItemTemplate
属性中声明一个
DataTemplate
,如果您只想显示一个值,甚至只需将
DisplayMemberPath
设置为相关的
Course
属性。

不要认为
CompositeCollection
甚至支持直接排序/筛选/分组,而无需自己对内容进行子类化或处理。难道你不能只在VM集合中添加一个空项来实现你想要的,而不使用
CompositeCollection
?这样,只有虚拟机中的集合才有虚拟元素,而没有模型。如果您想要更好的分离,可以使用接口实现
ItemSource
,并让虚拟项和
课程
实现该接口,以便以后轻松区分它们。
_courses.Add(new Course());