如何使用相对绑定将IsEnabled传递给模板的子代?

问题描述

我有以下XAML代码

    <t:LinkTemplate Text="ABC" IsEnabled="{Binding IsRenEnabled}"/>

及其调用的模板:

    public class LinkTemplate : Grid
    {
        public LinkTemplate()
        {
            this.ColumnDeFinitions.Add(new ColumnDeFinition() { Width = new GridLength(1,GridUnitType.Star) });
            LinkLabel LL = new LinkLabel()
            .Bind(LinkLabel.TextProperty,nameof(Text),source: this)
            this.Children.Add(LL,0);
        }
   }

由于LL具有IsEnabled属性,我想做的是将IsEnabled的{​​{1}}向下传递到LinkTemplate

因此,如果IsRenEnabled为true,则LL的IsEnabled为true。

有人可以给我建议我如何使用相对绑定吗?例如,我可以像在同一时间为文本添加绑定一样添加绑定吗?

解决方法

public LinkTemplate()
{
    this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1,GridUnitType.Star) });
    Label LL = new Label();
    LL.BindingContext = this;
    LL.SetBinding(Label.IsEnabledProperty,Label.IsEnabledProperty.PropertyName);

    this.Children.Add(LL,0);
}

设置Label的BindingContext属性和IsEnabled属性的SetBinding。因此,当您更改LinkTemplate的IsEnbaled属性时,Label IsEnabled属性也将更改。