数据上下文背后的代码为空,即使其值显示在页面上 - Windows Phone 8.1

Code behind Data Context is null, even though its values are showing on the page - Windows Phone 8.1

我有一个包含用户控件的页面,页面的数据上下文已绑定,包含的用户控件也是如此。显示页面时,我可以看到页面中的用户控件显示了我所期望的元素的值,例如标题和描述,但是,在同一用户控件的代码隐藏中,数据上下文是null..我做错了什么?

我需要数据上下文,以便我可以在控件中编辑它的值或更改页面中的其他 UI 元素。我在这里做错了什么?

我的页面:

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding}"

对该页面使用用户控件:

<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
       <local:ucFooControl DataContext="{Binding}"/>
</Grid>

然后,我在用户控制代码隐藏中看到空值,即使显示元素正在显示绑定项。

public ucFooControl()
{
    this.InitializeComponent();

    if (this.DataContext != null)
    {
        bar= (Bar)DataContext;
        this.DoSomething((bar);
    }
}

试试这个:

        public MyUserControl1()
        {
            this.InitializeComponent();
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
        {
            if (this.DataContext != null)
            {
               bar = (Bar)DataContext;
               this.DoSomething((bar);
            }
        }