I want to use Trigger for switch TextColor depending on BindableProperty of my class.
On click the button TextColor becomes Red. But on next click TextColor stay Red although BindableProperty switched to false.
Is it bug or my misunderstanding?
P.S. This example was simpled, really it uses with ListView & ItemsSource for Items, but this example also shows my trouble
XAML:
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Highlighted}" Value="true">
<Setter Property="TextColor" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Label Text="SingleLabel" x:Name="labelSingle" />
<Button Text="Switch" x:Name="bt" Clicked="bt_Clicked" />
</StackLayout>
</ContentPage.Content>
XAML.cs:
private MyItem myItem = new MyItem();
public List()
{
InitializeComponent();
this.BindingContext = myItem;
}
public void bt_Clicked(object sender, EventArgs e)
{
myItem.Highlighted = !myItem.Highlighted;
}
MyItem class:
public class MyItem : BindableObject
{
public string Name { get; set; }
public static readonly BindableProperty HighlightedProperty =
BindableProperty.Create<MyItem, bool>(w => w.Highlighted, false);
public bool Highlighted {
get { return (bool)GetValue(HighlightedProperty); }
set { SetValue(HighlightedProperty, value); }
}
}