Is it possible to bind to the Title of a Xarmarin ContentPage?

PaulReadPaulRead ✭✭GBMember ✭✭

I have a ContentPage showing some messages, I wish for the page title to dynamically reflect the number of unread messages e.g. 'Messages (5)' indicating the user has 5 unread messages (a useful indicator as the unread messages may have fallen off the bottom of the view)

I was hoping to do something like this:

MyContentPage.BindingContext=App.Instance.ViewModel; MyContentPage.SetBinding(ContentPage.TitleProperty,"UnReadMsgs");

but there appears to be no ContentPage.TitleProperty.

I tried using OnAppearing which is of limited success as the Messages can be updated without the page being reloaded

Would love to have some suggestions on how I can create this functionality in Xamarin Forms

Tagged:

Best Answer

Answers

  • PaulReadPaulRead ✭✭ GBMember ✭✭
  • DFoulkDFoulk ✭✭✭ USMember ✭✭✭

    For anyone using FreshMvvm and Code/C# pages:

        public class MyPage : ContentPage
        {
            public MyPage()
            {
                SetBinding(ContentPage.TitleProperty, new Binding("Title"));
    
                ...
            }
            ...
        }
    

    There was no need to manually set the BindingContext, nor was there a need to manually instantiate the Page/ViewModel in the page's class. Just simply add the above. Optionally (if you feel it's more readable), you may use this.SetBinding(...).

    Thanks @AlessandroCaliaro for pointing me down the right path!

  • ricardoventuraricardoventura ✭✭ Member ✭✭

    @DFoulk said:
    For anyone using FreshMvvm and Code/C# pages:

        public class MyPage : ContentPage
        {
            public MyPage()
            {
                SetBinding(ContentPage.TitleProperty, new Binding("Title"));
    
                ...
            }
            ...
        }
    

    There was no need to manually set the BindingContext, nor was there a need to manually instantiate the Page/ViewModel in the page's class. Just simply add the above. Optionally (if you feel it's more readable), you may use this.SetBinding(...).

    Thanks @AlessandroCaliaro for pointing me down the right path!

    i use your code but didnt work. didnt appear:

    SetBinding(TitleProperty, new Binding(myString));

    Can somebody help me changing the page title depending on which button I select?

  • DFoulkDFoulk ✭✭✭ USMember ✭✭✭
    edited April 15

    @ricardoventura said:

    @DFoulk said:
    For anyone using FreshMvvm and Code/C# pages:

        public class MyPage : ContentPage
        {
            public MyPage()
            {
                SetBinding(ContentPage.TitleProperty, new Binding("Title"));
    
                ...
            }
            ...
        }
    

    There was no need to manually set the BindingContext, nor was there a need to manually instantiate the Page/ViewModel in the page's class. Just simply add the above. Optionally (if you feel it's more readable), you may use this.SetBinding(...).

    Thanks @AlessandroCaliaro for pointing me down the right path!

    i use your code but didnt work. didnt appear:

    SetBinding(TitleProperty, new Binding(myString));

    Can somebody help me changing the page title depending on which button I select?

    Have you set your BindingContext? Please note, what I wrote did not set the BindingContext because that is handled by the MVVM framework that I was using (FreshMvvm).

    If you share snippets of your code and explain what framework (if any) you are using, I'm sure we could help you figure it out!

  • DFoulkDFoulk ✭✭✭ USMember ✭✭✭
    edited April 15

    @ricardoventura you can have a look at the documentation as well, which provides how to use Data Binding in C# (just set the appropriate BindingContext, then use the SetBinding() method to map the TitleProperty to whatever you want to bind it to (like a viewmodel property).

  • ricardoventuraricardoventura ✭✭ Member ✭✭

    @DFoulk said:
    @ricardoventura you can have a look at the documentation as well, which provides how to use Data Binding in C# (just set the appropriate BindingContext, then use the SetBinding() method to map the TitleProperty to whatever you want to bind it to (like a viewmodel property).

    Nevermind, i was complicating what i needed. I just needed to did this:

    public static Models.Manutencao _ManutencaoPag = new Models.Manutencao();
    public Manutencao2 (Models.Manutencao _ManutencaoPagina)
    {
    InitializeComponent ();
    _ManutencaoPag = _ManutencaoPagina;
    Title = _ManutencaoPag.Seccao;
    }

Sign In or Register to comment.