2012-06-29 72 views
0

我有这样的XAML代码:如何访问用户控件的父资源的WinRT XAML中

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

从用户控件的代码隐藏中,我怎么能访问CollectionViewSource?

回答

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

+0

我还没有机会尝试这个,但希望我以后会。看起来不错,谢谢! – 4imble

相关问题