1

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!

4

1 回答 1

0

我认为您不需要每次都重置源。你应该使用 this.TestCVS = CollectionViewSource.GetDefaultView(myCollection); 在加载的事件上,然后从 myCollection 添加和删除。您可以使用 ObservableCollection 免费获得更改通知。我没有彻底测试过这个想法,但它应该在理论上可行。

编辑:事实证明 GetDefaultView 在 Silverlight 中不存在,只有 WPF。我已成功使用 PagedCollectionView(myCOllection) 进行分组。

于 2010-05-13T16:13:40.440 回答