在 Silverlight 中使用 CollectionViewSource 时出现问题
我在 silverlight 中实现 CollectionViewSource 时遇到一些麻烦。我是这个主题的新手,所以基本上我一直在关注通过网络搜索找到的内容。到目前为止,这就是我一直在尝试做的事情。
我在资源标签中创建一个 CollectionViewSource:
<UserControl.Resources>
<CollectionViewSource x:Key="TestCVS">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Value" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
然后我将我的 TestCVS 绑定在 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>
现在,在后面的代码中,我在属性中为 TestCVS 分配源,如下所示:
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;
}
}
经过测试后,我意识到该信息没有显示在屏幕上,我真的不知道为什么,有人可以帮助我解决这个问题吗?
希望这有意义,提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要每次都重置源。您应该使用 this.TestCVS = CollectionViewSource.GetDefaultView(myCollection);在加载的事件上,然后添加和从 myCollection 中删除。您可以通过 ObservableCollection 免费获得更改通知。我还没有彻底测试这个想法,但理论上它应该可行。
编辑:
事实证明 GetDefaultView 在 Silverlight 中不存在,只有 WPF 中存在。我已成功使用 PagedCollectionView(myCOllection) 进行分组。
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.