Hi,
Ive a ListView with ViewCell which contains a Label and a Button, my List is for showing Todo Items and the Button is to mark the Todo Item done and show the status on the Button as Text (with a Unicode Checkmark or Cross).
So I want to change the Button Text when a bool attribute binded to a DataTrigger raises PropertyChaged.
My Model Class "TodoItem" implements the IPropertyChanged interface and my "TodoItemViewModel" has a OberservableCollection with TodoItems and implements the IPropertyChanged interface too ( Iam using the NuGet package "PropertyChanged.Fody" to implement the interface).
For Android and iOS its working perfectly, when Iam clicking the Button of a ListView Item, the "done" Attribute of the TodoItem in the OberservableCollection gets changed to "true" and so the PropertyChaged Event gets triggert and causes the Button DataTrigger in XAML to correctly update the Text of the clicked Button.
But on UWP the Button Text sometimes seems to do not update correctly and gets Empty (normally it sould show a Checkmark or a Cross).
Here is my XAML snippet:
<ListView x:Name="listView" ItemsSource="{Binding TodoItems}" HasUnevenRows="true" Margin="5">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" BackgroundColor="LightGray">
<Label Text="{Binding name}" FontSize="Large" FontAttributes="Bold" HorizontalOptions="CenterAndExpand"/>
<Button Clicked="doneButton_Clicked" CommandParameter="{Binding .}" HorizontalOptions="End" >
<Button.Triggers>
<DataTrigger TargetType="Button" Binding="{Binding done}" Value="true">
<Setter Property="Text" Value="✔"/>
</DataTrigger>
<DataTrigger TargetType="Button" Binding="{Binding done}" Value="false">
<Setter Property="Text" Value="✖"/>
</DataTrigger>
</Button.Triggers>
</Button>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here is the TodoItemViewModel which is binded as BindingContext in "OnAppearing" method in the CodeBehind of the XAML :
[AddINotifyPropertyChangedInterface] //From NuGet PropertyChanged.Fody
public class TodoItemViewModel
{
public ObservableCollection<TodoItem> TodoItems { get; set; } //Binded to ListView.ItemsSource
public TodoItemModel()
{
}
}
Here ist the method for Button Clicked:
private async void doneButton_Clicked(object sender, EventArgs e)
{
Button btn = sender as Button;
TodoItem todoItem= btn.CommandParameter as TodoItem;
if (todoItem != null && btn != null && btn.IsEnabled && btn.IsVisible)
{
if (!todoItem.done)
{
todoItem.done = true;
}
else
{
todoItem.done = false;
}
await database.Add(todoItem);
}
}
Here is a screenshot of the ListView on UWP (Problem marked with blue circle):
I hope someone can help me with this annoying problem on UWP...
Thanks!
Regards,
Tom