开发者

Trouble using CollectionViewSource in Silverlight

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 tes开发者_开发问答tCVS = (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!


I don't think you need to reset the source each time. You should use this.TestCVS = CollectionViewSource.GetDefaultView(myCollection); on a loaded event and then add and remove from myCollection. You get change notification for free with ObservableCollection. I've not tested this idea thoroughly, but it should work in theory.

EDIT: It turns out the GetDefaultView does not exist in Silverlight, only WPF. I've used the PagedCollectionView(myCOllection) successfully for grouping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜