如何在WinRt Xaml中访问UserControl的父资源

时间:2012-06-29 15:28:10

标签: c# .net xaml windows-runtime winrt-xaml

我有这个xaml代码:

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

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

1 个答案:

答案 0 :(得分:2)

  1. collectionviewsource绑定到Tag并从后面的代码访问

    < 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框架进行视图之间(或视图模型之间)的通信。