locked
AppBar\CommandBar DataContext nullified

    Question

  • Hi there,

    I have the following simple scheme: Page contains AppBar(or CommandBar, it doesn't matter):

    <Page.BottomAppBar>
            <CommandBar x:Name="appbar" >
                <AppBarButton Icon="Sort" Label="Sort" Click="onClick"/>
            </CommandBar>
    </Page.BottomAppBar>

    And I'm trying to set DataContext property in different places:

    • Code behind: in page constructor and OnNavigatedTo method
    • XAML: DataContext="{Binding DataContext, ElementName=SomeItem} where SomeItem is valid item of the page

    And when I click on button(onClick) I have appBar->DataContext == nullptr inside it. Only if I set this DataContext in onClick will it remain set till the end.

    So the questions are: Why is it nullified? And how should  I do it correctly?


    • Edited by ixSci Thursday, December 12, 2013 8:14 AM
    Thursday, December 12, 2013 8:13 AM

Answers

  • I found the event where I can set it: Loaded. But still it doesn't work with XAML binding, so I have to use code behind file in this simple case. So really it may be marked as solved but I'd still like to know what is going on and why XAML binding doesn't work in this case.

    • Edited by ixSci Tuesday, January 21, 2014 9:35 AM
    • Marked as answer by ixSci Tuesday, January 21, 2014 12:03 PM
    Tuesday, January 21, 2014 9:35 AM

All replies

  • What's the actual code of the setting of the DataContext setting in OnNavgiated?

    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.

    Friday, December 13, 2013 12:57 PM
    Moderator
  • void MainPage::OnNavigatedTo(NavigationEventArgs^)
    {
        appbar->DataContext =  someObject;
    }

    I'm setting it right and I'm sure of it. I checked it right after the setting and as I mentioned above if I set it in button Click handler then it will persist. Something nullifies it after OnNavigateTo called.
    Friday, December 13, 2013 4:47 PM
  • Did you get this sorted out?  If not, can you show an app that repros the problem?

    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, January 13, 2014 4:27 PM
    Moderator
  • I found the event where I can set it: Loaded. But still it doesn't work with XAML binding, so I have to use code behind file in this simple case. So really it may be marked as solved but I'd still like to know what is going on and why XAML binding doesn't work in this case.

    • Edited by ixSci Tuesday, January 21, 2014 9:35 AM
    • Marked as answer by ixSci Tuesday, January 21, 2014 12:03 PM
    Tuesday, January 21, 2014 9:35 AM