简体   繁体   English

如何获取XAML中定义的CollectionView

[英]How can I get the CollectionView that is defined in XAML

I wanted to bind to an ObservableCollection in XAML and also apply the grouping there. 我想绑定到XAML中的ObservableCollection并在那里应用分组。 In principle, this worked fine. 原则上,这很好。

<UserControl.Resources>
    <CollectionViewSource x:Key="cvs" Source="{Binding Path=TestTemplates}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Title"/>
        </CollectionViewSource.SortDescriptions>
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="TestCategory"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

Then the data binding expression became ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}" instead of ItemsSource="{Binding Path=TestTemplates}" . 然后数据绑定表达式变为ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}"而不是ItemsSource="{Binding Path=TestTemplates}"

At first, everything seemed cool, until I wanted to refresh the UI from the view model. 起初,一切看起来都很酷,直到我想从视图模型中刷新UI。 The problem is, that CollectionViewSource.GetDefaultView(TestTemplates) returned a different view than the one from XAML where the grouping was applied. 问题是, CollectionViewSource.GetDefaultView(TestTemplates)返回的视图与应用分组的XAML中的视图不同。 Thus, I could not set selection or do anything useful with it. 因此,我无法设置选择或做任何有用的事情。

I could fix it by binding the list again directly to the view model's property and setting up the grouping in the code-behind. 我可以通过将列表再次直接绑定到视图模型的属性并在代码隐藏中设置分组来修复它。 But I'm not that happy with this solution. 但我对这个解决方案并不满意。

private void UserControlLoaded(object sender, RoutedEventArgs e)
{
    IEnumerable source = TemplateList.ItemsSource;
    var cvs = (CollectionView)CollectionViewSource.GetDefaultView(source);
    if (cvs != null)
    {
        cvs.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));
        cvs.GroupDescriptions.Add(new PropertyGroupDescription("TestCategory"));
    }
}

I assume, the reason for that is already given by John Skeet here . 我认为,其原因已由John Skeet在此提供

Nevertheless, I would expect that there should be a way to get the right view. 尽管如此,我希望应该有一种方法来获得正确的观点。 Am I wrong? 我错了吗?

You could not just do that? 你不能这样做吗?

var _viewSource = this.FindResource("cvs") as CollectionViewSource;

If the data is connected, I assume that will have an updated view. 如果数据已连接,我认为将有更新的视图。

I tend to just expose the collection view from the VM rather than have the view define it: 我倾向于只从VM公开集合视图而不是让视图定义它:

public ICollection<Employee> Employees
{
    get { ... }
}

public ICollectionView EmployeesView
{
    get { ... }
}

That way your VM has full control over what is exposed to the view. 这样,您的VM就可以完全控制暴露给视图的内容。 It can, for example, change the sort order in response to some user action. 例如,它可以响应某些用户操作更改排序顺序。

Found a way, based on J. Lennon's answer. 找到了一种方法,基于J. Lennon的回答。 If I pass something that has access to the resources with my command, then I can look up the CollectionViewSource there. 如果我通过my命令传递了可以访问资源的东西,那么我可以在那里查找CollectionViewSource

In XAML ( CollectionViewResource as above): 在XAML(如上所述的CollectionViewResource )中:

<Button Command="{Binding Command}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">Do it!</Button>

And in the VM code: 在VM代码中:

private void Execute(object parm)
{
    var fe = (FrameworkElement)parm;
    var cvs = (CollectionViewSource)fe.FindResource("cvs");
    cvs.View.Refresh();
}

The Execute is the one that is given to the RelayCommand . Execute是给RelayCommand的那个

This would answer the question, but I don't like it very much. 这会回答这个问题,但我不太喜欢它。 Opinions? 意见?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取程序集中定义的XAML资源列表? - How to get a list of XAML resources defined in an Assembly? 如何获取XAML中定义的WrapPanel的尺寸 - How to get dimensions of WrapPanel defined in XAML 如何在XAML中获取图像以实际尺寸显示? - How can I get images in XAML to display as their actual size? 如何使用 WPF XAML 获得插入阴影 - How can I get an inset drop shadow using WPF XAML 如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性? - How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml? 当应用程序启动时,如何处理XAML中定义的项目的添加? - How can I handle adding of the items, defined in XAML, when application starts? 如何使用矢量图形(在XAML中定义)作为功能区菜单项的图像? - How can I use vector graphics (defined in XAML) as the image of a Ribbon menu item? 我怎么知道WPF的XAML中已经定义了哪个VisualState控件? - How can I know which VisualState of control already defined in WPF's XAML? 如何在运行时将多次在XAML ResourceDictionary中定义的Path添加到WPF表单? - How can I add a Path, that has been defined in the XAML ResourceDictionary, multiple times to a WPF form at runtime? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM