4imble
4imble

Reputation: 14426

How to access a UserControl's parent resources in WinRt Xaml

I have this xaml code:

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

From within the codebehind of the UserControl, how can i access the CollectionViewSource?

Upvotes: 0

Views: 2387

Answers (1)

Tilak
Tilak

Reputation: 30728

  1. Bind collectionviewsource to Tag and access it from code behind

    < Full:TestSnapPage Name="MainView" Tag="{Binding Source={StaticResource cvs}}"/>
    
  2. Walk up to Parent page, and access resources in code behind

            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. Use MVVMLight framework for communication between views (or between view models).

Upvotes: 2

Related Questions