首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向控件模板提供BindingContext

如何向控件模板提供BindingContext
EN

Stack Overflow用户
提问于 2019-04-12 00:54:27
回答 3查看 1.5K关注 0票数 3

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

代码语言:javascript
复制
<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{Binding MyLabelText}"/>
        </ControlTemplate>
</ResourceDictionary>

我在主页上用它

代码语言:javascript
复制
<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"
            ControlTemplate="{StaticResource HomePageTemplate}">

</ContentPage>

我将HomepageHomepage设置为后面的代码。

现在ControlTemplate不应该继承主页的BindingContext吗?因为我认为是这样的,但是我的Label并没有阻止MyLabelText的文本。在这些模板中使用Bindings需要做些什么?

编辑:

使用此选项

代码语言:javascript
复制
<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{TemplateBinding Parent.BindingContext.MyLabelText}"/>
        </ControlTemplate>
</ResourceDictionary>

也不适用于我,因为我在HomePage的头中使用了HomePage,而不是在它的体内。

这是有效的,,但我要寻找的不是

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-12 02:33:50

对于ControlTemplate控件,绑定略有不同。看看这些文档:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding

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

代码语言:javascript
复制
<ResourceDictionary>
        <ControlTemplate x:Key="HomePageTemplate">
            <Label Text="{TemplateBinding Parent.BindingContext.MyLabelText }"/>
        </ControlTemplate>
</ResourceDictionary>
票数 4
EN

Stack Overflow用户

发布于 2022-07-04 10:13:35

App.xaml应该有一个BindingContext。让我们称它为AppViewmodel,在App.xaml顶部定义一个名称空间vm (或任何其他键)。这个AppViewmodel可以从ControlTemplate中绑定到。在本例中,一个按钮(放置在堆栈布局中)绑定到一个名为AppCommand的命令,该命令驻留在AppViewmodel中。当此ControlTemplate应用于ContentPage中时,它绑定到ViewModel of App.xaml,而不是绑定到特定ContentPage的VM。也许后者也是可能的,例如,通过为RelativeSource和AncestorType选择正确的设置,但我还没有弄清楚这一点。另请参阅

代码语言:javascript
复制
     <ControlTemplate x:Key="HomePageTemplate">
            <StackLayout BindingContext="{Binding Source={RelativeSource TemplatedParent}}">
                    <Button 
Command="{Binding Source={RelativeSource AncestorType={x:Type vm:AppViewModel}}, Path=AppCommand}"
                            />
            </StackLayout>
    </ControlTemplate>
票数 0
EN

Stack Overflow用户

发布于 2022-07-05 03:32:38

如果有人感兴趣,它的工作方式就是在TemplateBinding之后添加路径,在它上指定BindingContext,然后是公共变量名:

代码语言:javascript
复制
<ResourceDictionary>
    <ControlTemplate x:Key="HomePageTemplate">
        <Label Text="{ TemplateBinding Path=BindingContext.MyLabelText }"/>
    </ControlTemplate>
</ResourceDictionary>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55643031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档