locked
Why would MessageDialog Change the DataContext of MenuflyoutItem to 'Null'?

    Question

  • I have a Button with a MenuFlyout and the DataContext  is fixed to some object, so when a MenuFlyoutItem is clicked, the DataContext of this MenuFlyoutItem is expected to be the same with the Button. That's true in most times, but today I encounted a weird thing: When I display a MessageDialog in the MenuFlyoutItem Clicked handler, the DataContext of the MenuFlyoutItem is changed 'Null' unexpectedly.Blow is the code:

    MainPage.xaml:

    <Page x:Class="Test.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Name="rootPage">
    
        <Grid Background="Black">
            <Button DataContext="{Binding ElementName=rootPage}"
                    Content="My button">
                <Button.Flyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="click me!"
                                        Click="MenuFlyoutItem_Click" />
                    </MenuFlyout>
                </Button.Flyout>
            </Button>
        </Grid>
    </Page>

    MainPage.xaml.cs:

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("old DataContext = " + (sender as FrameworkElement).DataContext);
    
                MessageDialog d = new MessageDialog("ok");
                await d.ShowAsync();
    
                Debug.WriteLine("new DataContext = " + (sender as FrameworkElement).DataContext);
            }
        }

    Output:

    old DataContext = Test.MainPage
    new DataContext =

    Is this behavior a bug of WinRT or just by design? thx!



    Monday, February 02, 2015 1:22 PM

Answers

  • Hi,

    I test your project and I can explain why. When you open the MessageDialog, you will find the MenuFlyoutItem LostFocus event can trigger, then MenuFlyout Closed event triggered. If the MenuFlyout closed, then resource bind to it are disposed. So the DataContext is empty.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Marked as answer by silverbird2015 Tuesday, February 03, 2015 5:58 AM
    Tuesday, February 03, 2015 5:44 AM

All replies

  • Hi,

    I test your project and I can explain why. When you open the MessageDialog, you will find the MenuFlyoutItem LostFocus event can trigger, then MenuFlyout Closed event triggered. If the MenuFlyout closed, then resource bind to it are disposed. So the DataContext is empty.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Marked as answer by silverbird2015 Tuesday, February 03, 2015 5:58 AM
    Tuesday, February 03, 2015 5:44 AM
  • Hi, Anne, thanks for the reply.It quite cleared my confusion.



    Tuesday, February 03, 2015 5:58 AM