Xamarin Forms - 绑定到ControlTemplate

Joh*_*ore 4 xaml xamarin xamarin.forms

我无法获取ControlTemplate中定义的绑定以对抗我的模型.

请注意,在下面的ControlTemplate中,我使用TemplateBinding绑定到名为Count(橄榄色标签)的属性.我正在使用本文规定的 Parent.Count ,但Parent.CountCount的值都不起作用.

在此输入图像描述

以下页面使用ControlTemplate.为了证明我的ViewModel正常工作,我还有一个灰色标签绑定到Count属性.

在此输入图像描述

注意结果屏幕.灰色标签显示Count属性.ControlTemplate中的橄榄标签没有显示任何内容.

在此输入图像描述

如何使ControlTemplate中的Label显示ViewModel中的Count属性?

查看模型

namespace SimpleApp
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            _count = 10;
            Uptick = new Command(() => { Count++; });
        }

        private int _count;
        public int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                OnPropertyChanged("Count");
            }
        }

        public ICommand Uptick { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SimpleApp"
             x:Class="SimpleApp.MainPage"
             ControlTemplate="{StaticResource ParentPage}">
    <StackLayout>
        <Button Command="{Binding Uptick}" Text="Increment Count" />
        <Label Text="{Binding Count}" BackgroundColor="Gray" />
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

代码背后

请注意,BindingContext在此处设置为MainViewModel.我需要使用自己的ViewModel而不是后面的代码.

namespace SimpleApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            BindingContext = new MainViewModel();

            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制模板

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleApp.App">
    <Application.Resources>

        <ResourceDictionary>
            <ControlTemplate x:Key="ParentPage">

                <StackLayout>
                    <Label Text="{TemplateBinding Parent.Count}" BackgroundColor="Olive" />
                    <ContentPresenter />
                </StackLayout>

            </ControlTemplate>
        </ResourceDictionary>

    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

Zro*_*roq 12

在ControlTemplate上,请使用以下代码:

<Label Text="{TemplateBinding BindingContext.Count}" BackgroundColor="Olive" />
Run Code Online (Sandbox Code Playgroud)

似乎BindingContext没有自动应用于您的ContentPage子项,也许它可能是Xamarin中的一个错误.