组合框空项目

时间:2014-01-21 21:43:46

标签: c# wpf combobox

我正在尝试将组合框与空项绑定。为此我正在使用CompositeCollection,正如我在许多问题中所读到的那样。

目前正在运作。然而,使用复合集合会混淆我所拥有的分组。要么我在组合框中得到空白,要么得到分组。我希望两者都可用。

以下是我一直在玩的示例代码:

的Xaml

<Window.Resources>
    <local:CourseConverter x:Key="CourseConverter" />
    <DataTemplate DataType="{x:Type local:Course}">
        <TextBlock Text="{Binding Path=CourseName}" />
    </DataTemplate>
    <CollectionViewSource x:Key="CoursesCVS" Source="{Binding Courses}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Major" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.2*"/>
        <RowDefinition Height="Auto "/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock Text="Select Course" Grid.Row="1" Margin="15,5,0,5" Width="200" />
    <ComboBox Grid.Row="2" Width="200" Margin="15,5,0,5"
              ItemsSource="{Binding Source={StaticResource CoursesCVS}}"
              SelectedItem="{Binding SelectedCourse, Converter={StaticResource CourseConverter}}">
        <ComboBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ComboBox.GroupStyle>
        <!--<ComboBox.ItemsSource>
            <CompositeCollection>
                <ComboBoxItem Content="" />
                <CollectionContainer Collection="{Binding Source={StaticResource CoursesCVS}}" />
            </CompositeCollection>
        </ComboBox.ItemsSource>-->
    </ComboBox>
    <TextBlock Text="{Binding SelectedCourse.CourseName}" Grid.Row="3" Margin="15,5,0,5" Width="200" />
</Grid>

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;
    }
}

是否有一些我缺少的东西让它可以使用和不分组。为了删除分组,我删除了内联ItemSource声明并使用注释代码。这显示空白。

1 个答案:

答案 0 :(得分:1)

在WPF中,我们使用数据项目,而不是 UI元素。我们声明DataTemplate来定义我们的数据项在任何UI容器中应该是什么样子。使用这种方法,我们让框架负责显示UI,我们专注于数据。因此,要在WPF中显示空UI元素,您只需在集合中添加一个空数据项,让DataTemplate完成其工作:

_courses.Add(new Course());

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