【问题标题】:Xamarin Forms - binding to a ControlTemplateXamarin Forms - 绑定到 ControlTemplate
【发布时间】:2017-02-13 21:17:36
【问题描述】:

我无法让 ControlTemplate 中定义的绑定对我的模型起作用。

请注意,在下面的 ControlTemplate 中,我使用 TemplateBinding 绑定到名为 Count(橄榄色标签)的属性。我将 Parent.Count 用作 prescribed by this article,但 Parent.Count 和 Count 的值都不起作用。

以下页面使用 ControlTemplate。只是为了证明我的 ViewModel 有效,我也有一个灰色的 Label 绑定到 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));
        }
    }
}

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>

代码隐藏

注意这里的 BindingContext 设置为 MainViewModel。我需要使用自己的 ViewModel 而不是背后的代码。

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

            InitializeComponent();
        }
    }
}

控制模板

<?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>

【问题讨论】:

  • 应该是 吗?
  • 我已经尝试过了,但它也不起作用。
  • 还要检查 NuGets 包和 Xamarin 本身的更新吗?我重新安装了我的电脑,现在这里没有 Xamarin。我想在这里测试。

标签: xaml xamarin xamarin.forms


【解决方案1】:

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

<Label Text="{TemplateBinding BindingContext.Count}" BackgroundColor="Olive" />

似乎 BindingContext 没有自动应用于您的 ContentPage 子项,可能是 Xamarin 中的错误。

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多