【问题标题】:CollectionViewSource Use QuestionCollectionViewSource 使用问题
【发布时间】: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


    【解决方案1】:

    这应该适合你

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

    【讨论】:

    • 有一天我也会变得聪明,只知道这种东西! (感谢您的帮助!)
    • 它对我也有帮助...但是为什么这行得通,而原始变体却行不通? :)
    • @DmitryLobanov 基本上 CollectionViewSource 本身并不是一个集合,但它可以用来访问一个集合。这可能会帮助zagstudio.com/blog/387#.UyC1nPldV-4
    • 我不断地寻找这些答案,以找到每种情况的神奇组合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多