我有一个看起来像这样的标签:
<Label Style="{StaticResource myStyle}" x:Name="TestLabel">
绑定样式将标签的背景颜色设置为红色。
然后在代码中,我这样做:
TestLabel.SetValue(Label.BackgroundColorProperty, Color.Green);
这会将标签的背景颜色设置为绿色。
但是,我需要清除我手动设置的绿色,我需要将其重置为默认值,由绑定样式(在本例中为红色)决定。
所以我这样做:
TestLabel.ClearValue(Label.BackgroundColorProperty);
但相反,恢复为红色,背景颜色将被完全删除并变为透明(这是标签的默认设置,不附加任何样式)。
(提供自己的一个解决方案是TestLabel.SetValue(Label.BackgroundColorProperty,Color.Red);但我无法做到这一点,因为我需要将颜色恢复为附加到其上的任何样式所说的它是,因为不同的标签附有不同的样式)
答案 0 :(得分:0)
设置或重置属性的最佳方法是使用触发器而不是手动设置。
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/triggers/