locked
Object reference error when trying to use CollectionViewSource RRS feed

  • Question

  • Hi all.  I'm working in Expression Blend 4, Silverlight+Sketchflow.  I am trying to use a CollectionViewSource in order to provide sorting and filtering.  

    The error message is "Cannot resolve reference assemblies.  Please check the reference assemblies.  Object reference not set to an instance of an object"

     

     

    <UserControl
    
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    
    	xmlns:local="clr-namespace:EagleObjectCodeAssignmentWithGridScreens"
    
    	xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    
    	xmlns:pc="http://schemas.microsoft.com/prototyping/2010/controls"
    
    	xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    
    	xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:pi="http://schemas.microsoft.com/prototyping/2010/interactivity"
    
    	xmlns:compMod="clr-namespace:System.ComponentModel;assembly=System.Windows"
    
    	xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    
    	xmlns:data="clr-namespace:System.Windows.Data;assembly=System.Windows"
    
    	mc:Ignorable="d"
    
    	x:Class="EagleObjectCodeAssignmentWithGridScreens.SingleAccountMapping"
    
    	Width="900" Height="600">
    
    	<UserControl.Resources>
    
    			
    
    		
    
    		<ResourceDictionary> 
    
    			<data:CollectionViewSource Source="{Binding ItemCollection}" x:Key="CollectionView1"/> 
    

     

    If I remove the reference to the collection view, the project builds successfully.  (Not sure if it matters for this error but my data is being imported from XML and can't be defined locally).

     

    Any help would be greatly appreciated!

    Friday, February 3, 2012 8:57 PM

All replies

  • This xaml works fine for me, so your xaml should be ok. Did you add a reference to a WPF/.Net assembly instead of a Silverlight one accidentally? Can you provide more details on the error message?

    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	mc:Ignorable="d"
    	x:Class="SilverlightPrototype2Screens.Screen_1"
    	xmlns:data="clr-namespace:System.Windows.Data;assembly=System.Windows"
    	Width="640" Height="480">
    	<UserControl.Resources>
    		<CollectionViewSource x:Key="CollectionView1" />
    		<data:CollectionViewSource x:Key="CollectionView2" />
    	</UserControl.Resources>
    
    	<Grid x:Name="LayoutRoot" Background="White"/>
    </UserControl>
    


    Saturday, February 4, 2012 3:52 PM
    Moderator
  • Thanks Chuck.  I was playing around with this some more and think I figured out the problem...I resolved this error by moving the CollectionViewSource statement  below the <ResourceDictionary.MergedDictionaries> section.  However, I still can't get the collection view to work.  Here's another sample I've been messing around with, where I'm trying to display the the "Name" field in a listbox.  My ultimate goal is to use the collection view to filter the contents of this data presented in a tree view, but I'm not having much luck understanding how it works.

     

    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	mc:Ignorable="d"
    	x:Class="SilverlightPrototype2Screens.Screen_2"
    	Width="900" Height="600">
    	
    	<UserControl.Resources>
    		<ResourceDictionary>
    				
    			<!--CollectionViewSource statement was here, which caused a Reference Assembly error-->
    						
    			<ResourceDictionary.MergedDictionaries>
    				<ResourceDictionary Source="ProjectDataSources.xaml"/>
    			</ResourceDictionary.MergedDictionaries>
    			
    			<CollectionViewSource Source="{Binding Accounts}" x:Key="AccountsCV"/>		
    			
    			<DataTemplate x:Key="ItemTemplate">
    				<StackPanel>
    					<TextBlock Text="{Binding Name}" Style="{StaticResource BasicTextBlock-Sketch}"/>
    				</StackPanel>
    			</DataTemplate>
    		</ResourceDictionary>
    	</UserControl.Resources>
    
    	<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource AccountsCV}}">
    		<ListBox HorizontalAlignment="Left" 
    			Margin="112,136,0,120" Width="232" 
    			ItemTemplate="{StaticResource ItemTemplate}" 
    			ItemsSource="{Binding ItemCollection}"/>
    	</Grid>
    </UserControl>

    and this is my data source:

    Saturday, February 4, 2012 4:51 PM
  • What part isn't working currently? Wrong thing displayed? Nothing displayed? 
    Saturday, February 4, 2012 5:37 PM
    Moderator
  • Sorry, forgot to mention that...nothing is displayed in the listbox
    Saturday, February 4, 2012 5:51 PM
  • So it looks like from your image you have 2 levels of collections, the main collection, which then contains items with the own collections, which have a name property. Your listbox is presumably bound to ItemCollection on the main data source, assuming the names line up (AccountsCV is defined someplace).

    So the items in your listbox have the properties Description and ItemCollection. Try changing Name to Description and see if you get valid data there.

    I'm definitely not an expert on the filtering stuff, but if you provide some details about exactly what you are trying to accomplish I can probably help you sort it out. Another idea would be to use a value converter on the ItemCollection binding to return all the names, if that is what you really want. 

    Saturday, February 4, 2012 5:54 PM
    Moderator
  • I tried changing Name to Description and the list box was still empty.  AccountsCV isn't defined anywhere else...I'm lost on how to bind the data correctly.  I was trying to wrap Accounts with AccountsCV and then reference that in the listbox.

    As a reference, I'm including the XAML from ProjectDataSources below (that's the extent of this project).  

     

     

    <ResourceDictionary
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    	xmlns:Sampledata="clr-namespace:Expression.Blend.SampleData.Accounts" 
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    	mc:Ignorable="d">
    	<!-- Resource dictionary entries should be defined here. -->
    	<Sampledata:AccountSource x:Key="Accounts" d:IsDataSource="True"/>
    </ResourceDictionary>
    

     

    Once I get the basics of this, then I'll work on filtering...


    • Edited by davidep411 Sunday, February 5, 2012 1:28 AM
    Sunday, February 5, 2012 1:26 AM
  • I think you should be able to drag from itemCollection in the data panel onto a listbox, and it will set up the basics of the binding for you. That might be a good starting place to explore how it should be set up.

     

    In these lines of XAML:

    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource AccountsCV}}">
    		<ListBox HorizontalAlignment="Left" 
    			Margin="112,136,0,120" Width="232" 
    			ItemTemplate="{StaticResource ItemTemplate}" 
    			ItemsSource="{Binding ItemCollection}"/>
    	</Grid>
    The datacontext of the grid is set to "AccountsCV", if that isn't defined anywhere, it will fail to bind and nothing will work. Whatever that is bound to has to exist someplace in your project. I would guess the grid should not have a datacontext, and the listbox should have ItemsSource bound to Accounts.

    Sunday, February 5, 2012 4:24 PM
    Moderator