【问题标题】:How can I change the color of a label depending on if isEnabled is true or false?如何根据 isEnabled 是真还是假来更改标签的颜色?
【发布时间】:2020-11-05 16:35:28
【问题描述】:

我正在使用的模板中有此代码,但由于此消息它无法工作:

我了解为什么会出现该消息,但我不知道如何解决此问题,以便标签颜色取决于 isEnabled 的状态。

namespace Templates
{
    public class LinkLabel : Label
    {
        public LinkLabel()
        {
            this.SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
            this.SetDynamicResource(Label.FontSizeProperty,   "LabelTextFontSize");
            this.SetDynamicResource(Label.TextColorProperty,  "LinkLabelColor");
            VerticalTextAlignment = TextAlignment.Center;
            VerticalOptions = LayoutOptions.CenterAndExpand;
        }

        public static BindableProperty IsEnabledProperty =
    BindableProperty.Create(nameof(IsDisabled), typeof(bool), typeof(LinkLabel), default(bool), propertyChanged: IsEnabledPropertyPropertyChanged);

        public bool IsDisabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        private static void IsEnabledPropertyPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if ((bool)newValue)
            {
                LinkLabel label = bindable as LinkLabel;
                label.SetDynamicResource(Label.TextColorProperty, "LinkLabelDisabledColor");
            }
            else
            {
                LinkLabel label = bindable as LinkLabel;
                label.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
            }
        }
    }
}

【问题讨论】:

  • 喜欢 BoolToColor converter?所以你可以在xaml中设置:color="{Binding boolvalue, Converter={StaticResource MyConverter}}"
  • 并非如此。我正在寻找基于 isEnabled 属性值设置颜色的 C# 解决方案。
  • "public static BindableProperty MyEnabledProperty", "BindingContext = this;"并在 xaml 集中:IsEnable="{Binding MyEnabled}"。像这样?

标签: xamarin xamarin.forms


【解决方案1】:

如果我们查看类 VisualElement 的源代码,我们会发现已经有一个名为 IsEnabledProperty 的可绑定属性。因为 VisualElement 是大多数 UI Elements 的超类。所以这两个IsEnabledProperty之间会有歧义。

解决方案 1:

最简单的方法是修改可绑定属性的名称,例如 IsDisabledProperty

解决方案 2:

因为 Label 中已经有一个 IsEnabledProperty 。我们可以在 xaml 中设置值并在后面的代码中处理逻辑,如

public class LinkLabel : Label
{
    public LinkLabel()
    {
        this.SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
        this.SetDynamicResource(Label.FontSizeProperty, "LabelTextFontSize");
        this.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
        VerticalTextAlignment = TextAlignment.Center;
        VerticalOptions = LayoutOptions.CenterAndExpand;

        this.PropertyChanged += LinkLabel_PropertyChanged;

    }

    private void LinkLabel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
       
        if(e.PropertyName== "IsEnabled")
        {
            var label = sender as LinkLabel;
            var newValue = label.IsEnabled;

            if ((bool)newValue)
            {
              
                this.SetDynamicResource(Label.TextColorProperty, "LinkLabelDisabledColor");
            }
            else
            {

                this.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
            }

        }

    }

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多