我有一个包含用户控件的页面,页面的数据上下文已绑定(bind),包含的用户控件也是如此。显示页面时,我可以看到页面中的用户控件显示了我所期望的元素的值,例如标题和描述,但是,在同一用户控件的代码隐藏中,数据上下文是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>
然后,我在用户控制代码隐藏中看到空值,即使显示元素正在显示绑定(bind)项。
public ucFooControl()
{
this.InitializeComponent();
if (this.DataContext != null)
{
bar= (Bar)DataContext;
this.DoSomething((bar);
}
}
我是一名优秀的程序员,十分优秀!