这是我的XAML代码:
<Label x:Name="faveLabel" FontFamily="FontAwesome"
XAlign="Center" FontSize="23">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Favorite}" Value="true">
<Setter Property="TextColor" Value="Red"/>
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Favorite}" Value="false">
<Setter Property="TextColor" Value="Gray"/>
</DataTrigger>
</Label.Triggers>
</Label>
我想做的是将其更改为C#
var label = new Label ()
{
FontFamily = "FontAwesome",
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 23,
}
var labelTrigger = new DataTrigger(label)
{
Target
}
我尝试在下面开始执行此操作,但在定义外部标签后不久我意识到我不知道如何定义触发器。任何人都可以就如何做到这一点给我建议吗?
答案 0 :(得分:3)
我没有对此进行测试,但这应该会让你走上正轨:
// Trigger for Favorite = true
DataTrigger triggerTrue = new DataTrigger(typeof(Label));
triggerTrue.Value = true;
triggerTrue.Binding = new Binding() { Path = "Favorite" };
Setter setterTrue = new Setter();
setterTrue.Property = Label.TextColorProperty;
setterTrue.Value = Color.Red;
triggerTrue.Setters.Clear();
triggerTrue.Setters.Add(setterTrue);
label.Triggers.Add(triggerTrue);
// Trigger for Favorite = false
DataTrigger triggerFalse = new DataTrigger(typeof(Label));
triggerFalse.Value = false;
triggerFalse.Binding = new Binding() { Path = "Favorite" };
Setter setterFalse = new Setter();
setterFalse.Property = Label.TextColorProperty;
setterFalse.Value = Color.Red;
triggerFalse.Setters.Clear();
triggerFalse.Setters.Add(setterFalse);
label.Triggers.Add(triggerFalse);
答案 1 :(得分:0)
带有触发器的ExtendedButton的示例,该触发器可根据IsEnabled属性更改按钮的样式:
public class ExtendedButton : Button
{
public static readonly BindableProperty EnabledStyleProperty =
BindableProperty.Create(nameof(EnabledStyle),
typeof(Style),
typeof(ExtendedButton),
default(Style),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnEnabledStyleChanged);
public static readonly BindableProperty DisabledStyleProperty =
BindableProperty.Create(nameof(DisabledStyle),
typeof(Style),
typeof(ExtendedButton),
default(Style),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnDisabledStyleChanged);
public ExtendedButton()
{
}
public Style EnabledStyle
{
get
{
return (Style)GetValue(EnabledStyleProperty);
}
set
{
SetValue(EnabledStyleProperty, value);
var enabledStyleTrigger = GetEnabledStyleTrigger();
this.Triggers.Clear();
this.Triggers.Add(enabledStyleTrigger);
}
}
public Style DisabledStyle
{
get
{
return (Style)GetValue(DisabledStyleProperty);
}
set
{
SetValue(DisabledStyleProperty, value);
var disabledStyleTrigger = GetDisabledStyleTrigger();
this.Triggers.Clear();
this.Triggers.Add(disabledStyleTrigger);
}
}
private static void OnEnabledStyleChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (ExtendedButton)bindable;
view.EnabledStyle = (Style)newValue;
}
private static void OnDisabledStyleChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (ExtendedButton)bindable;
view.DisabledStyle = (Style)newValue;
}
private Trigger GetDisabledStyleTrigger()
{
var disabledStyleTrigger = new Trigger(typeof(Button))
{
Property = Button.IsEnabledProperty,
Value = true
};
var buttonDisabledSetter = new Setter
{
Property = Button.StyleProperty,
Value = this.DisabledStyle
};
disabledStyleTrigger.Setters.Add(buttonDisabledSetter);
return disabledStyleTrigger;
}
private Trigger GetEnabledStyleTrigger()
{
var enabledStyleTrigger = new Trigger(typeof(Button))
{
Property = Button.IsEnabledProperty,
Value = true
};
var buttonEnabledSetter = new Setter
{
Property = Button.StyleProperty,
Value = this.EnabledStyle
};
enabledStyleTrigger.Setters.Add(buttonEnabledSetter);
return enabledStyleTrigger;
}
}
}