Trouble using CollectionViewSource in Silverlight

Posted by Johnny on Stack Overflow See other posts from Stack Overflow or by Johnny
Published on 2009-11-30T23:20:43Z Indexed on 2010/05/17 16:00 UTC
Read the original article Hit count: 242

Hi, I having some trouble when implementing the CollectionViewSource in silverlight. I'm new in this topic, so basically I've been following what I find searching through the web. Here's what I've been trying to do so far.

I'm creating a CollectionViewSource in the resources tag:

  <UserControl.Resources>
     <CollectionViewSource x:Key="TestCVS">
        <CollectionViewSource.SortDescriptions>
           <scm:SortDescription PropertyName="Value" Direction="Ascending" />
        </CollectionViewSource.SortDescriptions>
     </CollectionViewSource>
   </UserControl.Resources>

Then I'm binding my TestCVS in a HierarchicalDataTemplate:

<common:HierarchicalDataTemplate ItemsSource="{Binding Source={StaticResource TestCVS}}">
  <common:HierarchicalDataTemplate.ItemTemplate>
    <common:HierarchicalDataTemplate>
      <Border BorderBrush="#FF464646" BorderThickness="1" CornerRadius="3" Padding="5">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
            <TextBlock  TextWrapping="Wrap" Text="{Binding MyClassField}"/>
          </StackPanel>
        </Grid>
      </Border>
    </common:HierarchicalDataTemplate>
  </common:HierarchicalDataTemplate.ItemTemplate>
</common:HierarchicalDataTemplate>

Now, in the code behind I'm assigning the Source for the TestCVS in a property, like this:

private ObservableCollection<MyClass> _MyClass;
public ObservableCollection<MyClass> MyClass    
{
   get { return _MyClass; }
   set
   {
      var testCVS = (this.Resources["TestCVS"] as CollectionViewSource);

      if (testCVS != null)
        testCVS.Source = value;
   }
}

After testing this I realize that the information is not showing on screen and I don't really know why, can anyone help me on this matter?

Hope this makes any sense, thanks in advance!

© Stack Overflow or respective owner

Related posts about collectionviewsource

Related posts about Silverlight