2010-01-23 10 views
17

Intento hacer un uso básico de CollectionViewSource y me falta algo porque simplemente no funciona. Aquí está mi XAML:CollectionViewSource Use la pregunta

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

Este compila bien, pero cuando corro la aplicación me sale este 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 

Esta es la colección os adjunto a:

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

Solo quiero hacer una agrupación simple en el objeto Project SourceProject. Preferiría no tener que sacar una vista de árbol para esto.

Respuesta

38

Esto debería funcionar para usted

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

Algún día voy a ser demasiado inteligente y sólo sé este tipo de cosas! (¡Gracias por la ayuda!) – Vaccano

+1

Me ayudó también ... Pero ¿por qué funciona esto, y la variante original no funciona? :) –

+0

@DmitryLobanov Básicamente, un CollectionViewSource no es en sí mismo una colección, pero se puede usar para acceder a uno. Esto podría ayudar a http://www.zagstudio.com/blog/387#.UyC1nPldV-4 –

Cuestiones relacionadas