locked
Binding back to RelativeSource of Page?...

    Question

  • OS: Windows 8.1
    Target: Windows 8.1/Phone 8.1
    Project: Universal

    Need help solving a RelativeSource back to the Page DataContext to get my InvokeCommandAction to work from this:

    <prism:VisualStateAwarePage
    	...
    	..
    	.
    
        <Page.Resources>
            <DataTemplate x:Key="Standard310x260ItemTemplate">
    			...
            </DataTemplate>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
    		...
            <Hub DataContext="{Binding DefaultDataModel}">
    			...
                <HubSection IsHeaderInteractive="True" 
                            DataContext="{Binding Section3Items}" 
                            d:DataContext="{Binding Groups[3], Source={d:DesignData Source=../NathsarTS.Shared/DataModel/SampleData.json, Type=data:SampleDataSource}}"
                            x:Uid="Section3Header" Header="Section 3" Padding="40,40,40,32">
                    <DataTemplate>
                        <GridView
                            x:Name="itemGridView"
                            ItemsSource="{Binding Items}"
                            Margin="-9,-14,0,0"
                            AutomationProperties.AutomationId="ItemGridView"
                            AutomationProperties.Name="Items In Group"
                            ItemTemplate="{StaticResource Standard310x260ItemTemplate}"
                            SelectionMode="None"
                            IsSwipeEnabled="false"
                            IsItemClickEnabled="True">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="ItemClick">
                                    <core:InvokeCommandAction Command="{Binding HubSectionGridItemCommand, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </GridView>
                    </DataTemplate>
                </HubSection>
                <HubSection x:Uid="Section4Header" Header="Section 4">
    			...
                </HubSection>
            </Hub>
        </Grid>
    </prism:VisualStateAwarePage>

    My Page DataContext is a ViewModel, the <Hub DataContext="{Binding DefaultDataModel}"> is a Json data IObservableMap<string, object> collection for testing, HubSectionGridItemCommand is defined in my ViewModel.

    How do I get back to the Page DataContext?

    Thanks!...


    Code is like a box of chocolates!...


    • Edited by VcDeveloper Thursday, September 18, 2014 4:12 PM
    Thursday, September 18, 2014 4:11 PM

All replies

  • I'd love to help you with this but I just don't understand the question.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, September 22, 2014 12:56 PM
    Moderator
  • Hi,

    My Page.DataContext=PageViewModel, so this covers the whole XAML for Data Binding. My HubSection.DataContext="{Binding DefaultDataModel}", which is a IObservableMap<string, object> for temporary .json data for testing.  

    Since this HubSection.DataContext hides the Page.DataContext I no longer can reach my public DelegateCommand<object> HubSectionGridItemCommand { get; set; }.

    In WPF I can do this to get back to the Page.DataContext to reach my defined DelegateCommand:

    <Style TargetType="RibbonButton" x:Key="RibbonGroupViewCommandParameter"> <Setter Property="Command" Value="{Binding DataContext.RibbonGroupViewCommand, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
    ...

    </Style>


    But, with WinRT RelativeSource I'm limited to two option; self & templatedParent.  So is it possible to reach the root Page from a second, third,... level Control?


    Code is like a box of chocolates!...


    • Edited by VcDeveloper Wednesday, September 24, 2014 5:27 PM typos..
    Wednesday, September 24, 2014 7:26 AM
  • So I can assume there is no tracing back to the root Page in WinRT from a deep leveled Control like in WPF?

    Code is like a box of chocolates!...

    Sunday, September 28, 2014 5:44 PM
  • Hello, will this be fixed or considered in future versions?

    Code is like a box of chocolates!...

    Friday, October 03, 2014 11:28 PM
  • Hi - honestly I don't know that much about this area. If you'll upload an app that demonstratrates what you're doing, and then tell me what you want, I'll see what's going on and try to get your problem fixed.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, October 09, 2014 8:02 PM
    Moderator
  • Ok, I'll make a project with this problem, be right back.....

    Code is like a box of chocolates!...

    Friday, October 10, 2014 10:49 PM
  • Maybe try this differently. 

    Instead of having the hubsection binding to its own source, have it bind to an item within your page's PageViewModel. 

    In the constructor of the PageViewModel, do:

    DefaultViewModel.Add("hub", HubSection.DataModel); (or whatever it actually is in codebehind)

    Then, bind the page as you do normally.

    Bind the HubSection to the page's viewmodel, like this:

       DataContext="{Binding hub}";


    Darin R.

    Tuesday, October 14, 2014 1:44 PM
  • Hi Darin,

    Thanks for the reply!  I'll give it a try and get back with you....


    Code is like a box of chocolates!...

    Tuesday, October 21, 2014 3:28 AM