0

我有这个 xaml 代码:

<Common:LayoutAwarePage.Resources>
   <CollectionViewSource x:Name="cvs" IsSourceGrouped="true" />
</Common:LayoutAwarePage.Resources>
<Grid>
    <Full:TestSnapPage Name="MainView" />     
    ... 

从 UserControl 的代码隐藏中,我如何访问 CollectionViewSource?

4

1 回答 1

2
  1. 绑定collectionviewsource到标签并从后面的代码中访问它

    < Full:TestSnapPage Name="MainView" Tag="{Binding Source={StaticResource cvs}}"/>
    
  2. 走到父页面,并在后面的代码中访问资源

            var parentPage = GetParentsPage(this); 
            if (parentPage != null)
            {
               //parentPage.Resources["cvs"]
            }
    
            private ParentPage GetParentsPage(FrameworkElement @this)
            { 
                FrameworkElement temp = @this;
                while (temp.Parent != null)
                {
    
                    if (temp.Parent is ParentPage)
                        return temp.Parent as ParentPage;
    
                    temp = temp.Parent as FrameworkElement;
                }
                return null;
            }
    
  3. 使用MVVMLight框架在视图之间(或视图模型之间)进行通信。

于 2012-07-09T04:27:25.073 回答