我想根据另一个函数的结果使用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; }
我想根据另一个函数的结果使用label.datatriggers更改文本颜色。另外,如何访问每个命令,我想对每个被点击的位置执行一个操作。...
如何从IsToggle获得结果。