'How to set the ContentPage Title StringFormat through boolean binding and DataTriggers in Xamarin.Forms

I have created a "Mobile App (Xamarin.Forms)" project in VS 2019:

Project Selection

and then I chose the "Flyout" option:

Choosing Flyout Option

I modified two of the auto-generated classes: ItemDetailViewModel.cs and ItemDetailPage.xaml

ItemDetailViewModel.cs (Note my change to assign Title as well in the LoadItemId method)

using System;
using System.Diagnostics;
using Xamarin.Forms;

namespace TestDefaultAppSize.ViewModels
{
    [QueryProperty(nameof(ItemId), nameof(ItemId))]
    public class ItemDetailViewModel : BaseViewModel
    {
        private string itemId;
        private string text;
        private string description;
        public string Id { get; set; }

/* MY CODE STARTS HERE */
        public Command ExecuteSwitchTitleCommand { get; }

        public ItemDetailViewModel()
        {
            ExecuteSwitchTitleCommand = new Command(ExecuteSwitchTitle);
        }
        public void ExecuteSwitchTitle()
        {
            SwitchTitle = !SwitchTitle;
        }

        private bool switchTitle = false;
        public bool SwitchTitle
        {
            get => switchTitle;
            set => SetProperty(ref switchTitle, value);
        }

/* MY CODE ENDS HERE */

        public string Text
        {
            get => text;
            set => SetProperty(ref text, value);
        }

        public string Description
        {
            get => description;
            set => SetProperty(ref description, value);
        }

        public string ItemId
        {
            get
            {
                return itemId;
            }
            set
            {
                itemId = value;
                LoadItemId(value);
            }
        }

        public async void LoadItemId(string itemId)
        {
            try
            {
                var item = await DataStore.GetItemAsync(itemId);
                Id = item.Id;
                // ** I Added "Title = " so that the title would be set on load
                Title = Text = item.Text;
                Description = item.Description;
            }
            catch (Exception)
            {
                Debug.WriteLine("Failed to Load Item");
            }
        }
    }
}

ItemDetailPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="TestDefaultAppSize.Views.ItemDetailPage"
         Title="{Binding Title, StringFormat='{0} SWITCHED'}"> <!-- I bind to "Title" here -->

<!-- And add triggers for the Title to be set depending on "SwitchTitle" -->
     <ContentPage.Triggers>
    <DataTrigger TargetType="ContentPage" Binding="{Binding SwitchTitle}" Value="False">
        <Setter Property="Title" Value="{Binding Title}"/>
    </DataTrigger>
</ContentPage.Triggers>

    <StackLayout Spacing="20" Padding="15">
        <!-- and have added a button to toggle the boolean in the view model through a command-->
        <Button Text="{Binding SwitchTitle}" Command="{Binding ExecuteSwitchTitleCommand}"/>
        <Label Text="Text:" FontSize="Medium" />
        <Label Text="{Binding Text}" FontSize="Small"/>
        <Label Text="Description:" FontSize="Medium" />
        <Label Text="{Binding Description}" FontSize="Small"/>
    </StackLayout>
    
</ContentPage>

Now when I run this, I navigate to "Browse" from the hamburger menu and click on the first item. It opens up the ItemDetailPage, with the correct Title; however, when I click the button to toggle the formatting it appears as if the Title ends up empty or doesn't register properly. Here's the kicker, if I modify the StringFormat in the binding during debugging it starts to work. I can't for the life of me figure out what is actually going wrong and why this isn't actually formatting the string.

Here is a GIF of the process: Proof of issue

I've tried restarting visual studio and creating a new project, neither of which worked. I also tried only setting the binding for the Title through DataTriggers (one for SwitchTitle == true, and one for SwitchTitle == false), which also did not work.

If anyone has any suggestions or ideas as to what may be causing this it would be greatly appreciated.

EDIT: I just tried swapping the condition and it worked. Perhaps this is an issue with the underlying Page object and how string formatting is being applied to it? (See GIF) Somehow it works

Additional EDIT: Adding DataTrigger for each condition also does not work. It is a slightly different behavior that seems to get stuck eventually. See GIF attached:

    <ContentPage.Triggers>
        <DataTrigger TargetType="ContentPage" Binding="{Binding SwitchTitle}" Value="False">
            <Setter Property="Title" Value="{Binding Title, StringFormat='TEST {0} SWITCHED'}"/>
        </DataTrigger>
        <DataTrigger TargetType="ContentPage" Binding="{Binding SwitchTitle}" Value="True">
            <Setter Property="Title" Value="{Binding Title, StringFormat='{0} SWITCHED'}"/>
        </DataTrigger>
    </ContentPage.Triggers>

Two DataTriggers ends up getting stuck in a single state

Another EDIT: (to show more of what's going on with the weird behavior)

If I change the DataTriggers to reference a string value instead it is also showing a weird behavior. I have a DataTrigger for two different cases, and a default binding otherwise. If I start trying to match the values it eventually fails to revert back to the default binding. There are even cases where the value being checked does not equal the DataTrigger value and it still applies the condition. (See attached GIF) Text evaluations may be off as well

According to this excerpt from Microsoft, if the condition is no longer met, it should revert back to the default value (but it is not in my case):

Microsoft Documentation



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source