【发布时间】:2010-01-23 21:50:10
【问题描述】:
我正在尝试对 CollectionViewSource 进行基本使用,但我一定遗漏了一些东西,因为它无法正常工作。这是我的 XAML:
<Window.Resources>
<CollectionViewSource Source="{Binding loc:MainVM.Instance.MapItems}" x:Key="MapCV">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="SourceProject" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<ListBox ItemsSource="{StaticResource MapCV}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding SourceType, Converter={StaticResource WorkItemTypeToStringConverter}}"/>
<ComboBox Grid.Column="1" SelectedItem="{Binding DestType}" ItemsSource="{Binding WorkItemTypesForCurrentDestProject, Source={x:Static loc:MainMediator.Instance}, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Name" />
<Button Grid.Column="2" Content="{Binding PercentMapped}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这编译得很好,但是当我运行应用程序时,我得到了这个错误:
无法将属性“ItemsSource”中的值转换为类型的对象 'System.Collections.IEnumerable'。 'System.Windows.Data.CollectionViewSource' 不是属性“ItemsSource”的有效值。对象错误 标记文件“WIAssistant;component/main.xaml”中的“System.Windows.Controls.ListBox”这是我要附加的集合:
// The mappings used to copy the values of the fields of one WorkItem to another.
public ObservableCollection<WorkItemTypeMapping> WorkItemTypeMappings
{
get { return (ObservableCollection<WorkItemTypeMapping>)
GetValue(WorkItemTypeMappingsProperty); }
set { SetValue(WorkItemTypeMappingsProperty, value); }
}
public static readonly DependencyProperty WorkItemTypeMappingsProperty =
DependencyProperty.Register("WorkItemTypeMappings",
typeof(ObservableCollection<WorkItemTypeMapping>), typeof(MainMediator),
new UIPropertyMetadata(null));
我只想对对象Project SourceProject 进行简单的分组。我宁愿不必为此分解树视图。
【问题讨论】:
标签: c# .net wpf collectionviewsource