简体   繁体   English

如何在具有C#后端而不是XAML的模板上绑定XAML元素?

[英]How can I bind a XAML element on a template with the C# back end instead of in the XAML?

Here's what I have right now: 这是我现在所拥有的:

I have this template that's simplified for the question: 我有一个简化了此问题的模板:

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
   xmlns:t="clr-namespace:Japanese.Templates" 
   xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
   x:Class="Japanese.Templates.RoundButtonText" x:Name="this">
   <Label Text="ABC"     
          TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
   />
</Frame>

and this C# 和这个C#

using Xamarin.Forms;

namespace Japanese.Templates
{
    public partial class RoundButtonText : BaseFrameButtonTemplate
    {
        public RoundButtonText()
        {
            InitializeComponent();
            // I would like to put the Label TextColor binding here instead of in the XAML
        }

    }
}

Can someone help me by telling me how I can add the binding for the label TextColor in the C# back end so that it changes in exactly the same way as it currently does when it's written as: 有人可以告诉我如何在C#后端中添加标签TextColor的绑定,以便它以与当前编写时完全相同的方式更改:

 TextColor="{Binding LabelTextColor, Source={x:Reference this}}"

in XAML? 在XAML中?

Remove the binding from the XAML and give the Label control an x:Name: 从XAML删除绑定,并为Label控件指定一个x:Name:

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       xmlns:t="clr-namespace:Japanese.Templates" 
       xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
       x:Class="Japanese.Templates.RoundButtonText" x:Name="this">
    <Label x:Name="label" 
           Text="ABC" />
</Frame>

Set up the binding and the property to bind to in C# code behind: 在后面的C#代码中设置绑定和要绑定的属性:

using Xamarin.Forms;

namespace Japanese.Templates
{
    public partial class RoundButtonText : BaseFrameButtonTemplate
    {
        Color _labelTextColor;
        public Color LabelTextColor {
            get {
                return _labelTextColor;
            } 
            set {
                if (_labelTextColor != value) {
                    _labelTextColor = value;
                    OnPropertyChanged("LabelTextColor");
                } 
            }
        }

        public RoundButtonText()
        {
            InitializeComponent();
            label.BindingContext = this;
            label.SetBinding(Label.TextColorProperty, "LabelTextColor");
        }
    }
}

Now whenever the LabelTextColor property value is changed, the Label's TextColor property should change as well, just like with a XAML binding. 现在,每当LabelTextColor属性值更改时,Label的TextColor属性也应更改,就像使用XAML绑定一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将XAML网格的高度绑定回我的C#后端代码吗? - Can I bind the Height of a XAML Grid back to my C# back end code? 如何在 C# 而非 XAML 中定义静态资源? - How can I define a Static Resource in C# instead of XAML? 如何使用 Rg.Plugins.Popup 将 XAML 和模板合并到一个 C# 模板中? - How can I combine XAML and a template into one C# template with Rg.Plugins.Popup? 如果我在XAML中使用ContentPage.BindingContext定义绑定上下文,那么如何在C#后端访问它? - If I use ContentPage.BindingContext in XAML to define my binding context then how do I access that in the C# back end? 如何使用 C# 而不是 XAML 将两个 ResourceDictionary 对象合并到 Application.Current.Resources 中? - How can I merge two ResourceDictionary objects into Application.Current.Resources using C# instead of XAML? 如何使用 C# 而不是 XAML 向标签添加触发器? - How can I add a trigger to a label using C# instead of XAML? 如何将应用程序资源添加到应用程序的 C# 后端而不是 XAML 文件中? - How can I add application resources to my C# backend for the application instead of in the XAML file? 在 C# 而不是 XAML 中创建框架时,如何在构造函数中分配动态资源? - How can I assign a dynamic resource in the constructor when creating a frame in C# instead of XAML? 将 C# 模板添加到 XAML 代码。 我该如何进行绑定? - Adding a C# template to XAML code. How can I do the binding? 如何将 C# 生成的按钮绑定到 XAML MVVM - How to bind C# generated button to XAML MVVM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM