微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用相对绑定将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属性也将更改。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。