标签基于切换触发绑定 Textcolor

Label triggers binding Textcolor based on a toggle

我想根据另一个函数的结果使用 label.datatriggers 更改文本颜色。 此外,我如何访问每个命令,我想对每个点击执行一个操作。我可以在哪里添加此功能以及如何添加,如果我使用视图模型 - 如何从 IsToggle 获得结果。

if(IsToggle)
{
 //do logic 
} else
{
//do logic 
}

示例

    <Label Text="AUTO" HorizontalOptions="Center"
             VerticalOptions="Center">
              <Label.Behaviors>
               <local:ToggleBehavior x:Name="autoToggleBehavior" IsToggled="{Binding Toggled, Mode=TwoWay}"/>
             </Label.Behaviors>
             <Label.Triggers>
              <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="False"

                 <Setter Property="BackgroundColor" Value="White"/>
                 <Setter Property="TextColor" Value="Gray"/> 
               </DataTrigger>
               <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="True"

                <Setter Property="BackgroundColor" Value="Blue"/>
                 <Setter Property="TextColor" Value="White"/> 
               </DataTrigger>
             </Label.Triggers>
     </Label>

这是行为

public class ToggleBehavior : Behavior<View>
    {
        readonly TapGestureRecognizer tapRecognizer;

        public ToggleBehavior()
        {
            tapRecognizer = new TapGestureRecognizer
            {
                Command = new Command(() => this.IsToggled = !this.IsToggled)
            };


        }

        public static readonly BindableProperty IsToggledProperty = BindableProperty.Create<ToggleBehavior, bool>(tb => tb.IsToggled, false);

        public bool IsToggled
        {
            set { SetValue(IsToggledProperty, value); }
            get { return (bool)GetValue(IsToggledProperty); }
        }

        protected override void OnAttachedTo(View bindable)
        {
            base.OnAttachedTo(bindable);
            bindable.GestureRecognizers.Add(this.tapRecognizer);
        }
        protected override void OnDetachingFrom(View bindable)
        {
            base.OnDetachingFrom(bindable);
            bindable.GestureRecognizers.Remove(this.tapRecognizer);
        }
        protected override void OnAttachedTo(BindableObject bindable)
        {
            base.OnAttachedTo(bindable);
            this.BindingContext = bindable.BindingContext;
            bindable.BindingContextChanged += Bindable_BindingContextChanged;
        }
        protected override void OnDetachingFrom(BindableObject bindable)
        {
            base.OnDetachingFrom(bindable);
            this.BindingContext = null;
            bindable.BindingContextChanged -= Bindable_BindingContextChanged;
        }
        void Bindable_BindingContextChanged(object sender, EventArgs e)
        {
            var bobject = sender as BindableObject;

            this.BindingContext = bobject?.BindingContext;
        }

how can get the result from IsToggle.

您可以定义 IsToggledProperty 的事件 propertyChanged

    public static readonly BindableProperty IsToggledProperty = BindableProperty.Create("IsToggled", typeof(bool), typeof(ToggleBehavior), false,propertyChanged: OnIsToggledChanged);

    static void OnIsToggledChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var behavior = bindable as ToggleBehavior;
        bool Toggled = (bool)newValue ;

        if(Toggled)
        {

        }
        else
        {

        }


    }