我正在嘗試將一個空的項目與一個組合框綁定。爲此,我正在使用CompositeCollection
,因爲我已經閱讀了許多問題。Combobox Empty Item
目前工作正常。然而,使用複合集合會混淆我所擁有的分組。要麼我得到組合框中的空白,要麼我得到分組。我希望兩者都可用。
這裏是我一直在玩的示例代碼:
的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>
視圖模型和支持類:
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聲明並使用註釋的代碼。這顯示了空白。
不認爲''CompositeCollection'甚至可以直接支持排序/過濾/分組,直接處理子類或自己處理東西。難道你不能只在虛擬機集合中添加一個空項目來實現你的目標,而不是完全不使用CompositeCollection?這樣,只有虛擬機中的集合才具有虛擬元素,而不是您的模型,如果您想要更好地分離,可以使用接口實現您的'ItemSource',並讓Dummy項和'Course'實現該接口以輕鬆區分它們後來。 – Viv