Xamarin.forms 如何为控件模板提供BindingContext

Xamarin.forms 如何为控件模板提供BindingContext,xamarin.forms,binding,controltemplate,Xamarin.forms,Binding,Controltemplate,我在App.Xaml中定义了一个模板 <ResourceDictionary> <ControlTemplate x:Key="HomePageTemplate"> <Label Text="{Binding MyLabelText}"/> </ControlTemplate> </ResourceDictionary> 也不适用于我,因为我在主页的标题中使用控制模板,而不是在其

我在App.Xaml中定义了一个模板

<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{Binding MyLabelText}"/>
        </ControlTemplate>
</ResourceDictionary>
也不适用于我,因为我在
主页
的标题中使用
控制模板
,而不是在其正文中

这很有效,但不是我想要的:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
            xmlns:local="clr-namespace:App.Converters"
            x:Class="App.Views.HomePage"
            >
  <ContentView ControlTemplate="{StaticResource HomePageTemplate}" />
</ContentPage>

控件模板
控件绑定略有不同。看看这些文档:

假设
MyLabelText
属性是父控件的
BindingContext
的一部分,则代码可能如下所示:

<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{TemplateBinding Parent.BindingContext.MyLabelText }"/>
        </ControlTemplate>
</ResourceDictionary>

控件模板
控件绑定略有不同。看看这些文档:

假设
MyLabelText
属性是父控件的
BindingContext
的一部分,则代码可能如下所示:

<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{TemplateBinding Parent.BindingContext.MyLabelText }"/>
        </ControlTemplate>
</ResourceDictionary>


看一下,还有那里的样本。@JackHua MSFT你能看一下我编辑的问题吗?链接的thx请查看链接和示例。@JackHua MSFT您能看看我编辑的问题吗?链接的thx这不起作用,因为我的
ControlTemplate
位于ContentPage属性中,而不是正文中。仅当我将
ControlTemplate
放在主体内部时,此选项才有效work@Greggz你把这个标记为正确的?请解释,我和你有同样的问题。这不起作用,因为我的
ControlTemplate
在ContentPage属性中,而不是正文中。仅当我将
ControlTemplate
放在主体内部时,此选项才有效work@Greggz你把这个标记为正确的?请解释一下,我和你有同样的问题。
<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{TemplateBinding Parent.BindingContext.MyLabelText }"/>
        </ControlTemplate>
</ResourceDictionary>