本文介绍了CollectionViewSource使用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个基本的使用CollectionViewSource的,我必须失去了一些东西,因为它只是不工作。这是我的XAML:

I am trying to do a basic use of CollectionViewSource and I must be missing something because it is just not working. Here is my 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>

这个编译很好,但是当我运行该应用程序我得到这个错误:

This compiles fine, but when I run the app I get this error:


Cannot convert the value in attribute 'ItemsSource' to object of type 
'System.Collections.IEnumerable'. 'System.Windows.Data.CollectionViewSource' 
is not a valid value for property 'ItemsSource'.  Error at object 
'System.Windows.Controls.ListBox' in markup file 'WIAssistant;component/main.xaml

这是我附上的集合:

// 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));

我只想做简单的分组对象项目SourceProject 。我宁可不要打出来的树视图这一点。

I just want to do simple grouping on object Project SourceProject. I would rather not have to break out a tree view for this.

推荐答案

这应该为你工作。

<ListBox ItemsSource="{Binding Source={StaticResource MapCV}}" ...

这篇关于CollectionViewSource使用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:11