Forum Xamarin Xamarin.Forms

Default value for binded property

Hello,

I have a label which text property is binded to my model's property. I need to have a default property but couldn't find any way for this.

My model is like that :

using System;
using Newtonsoft.Json;

namespace MyProject.Models
{
    public class Intervention
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("participant")]
        public Contact Participant { get; set; }
    }
}

And for Participant :

using System;
using Newtonsoft.Json;

namespace MyProject.Models
{
    public class Contact
    {
        [JsonProperty("firstname")]
        public string FirstName { get; set; }

        [JsonProperty("lastname")]
        public string LastName { get; set; }

        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
    }
}

My form is like that :

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage ...>

    <ContentPage.BindingContext>
        <viewModels:InterventionViewModel x:Name="IVM"/>
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <StackLayout>

            <Label x:Name="ParticipantLabel" Text="{Binding Participant.FullName}" />

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The problem I'm facing is that Participant property can be null.

I've tried many things :

  • Adding DefaultValue and FallbackValue to the Binding but this doesn't get compiled
  • Adding a property Participant.FullNameOrNotAssigned that returns the default (localized) label if FullName is Null, which never get called when Participant is Null
  • Creating a converter EmptyStringToDefaultValueConverter that does the same, but never get called when Participant is Null as well

I've been looking also for some events when the Label get binded but didn't found any.

What should I do ?

Many thanks !

Posts

  • MulflarMulflar ESUniversity ✭✭
    edited January 2018

    Mmm... something like this?

    namespace MyProject.Models
    {
            public class Intervention
            {
                [JsonProperty("id")]
            public int Id { get; set; }
    
            [JsonProperty("participant")]
            public Contact participant { get; set; }
    
            [JsonIgnore]
                public Contact Participant { 
                get
                {
                    if(participant!=null)
                        return participant
                    else
                        return new Contact { FirstName ="blank", LastName ="blank"};
                }
                set
                {
                    participant = value;
                }
            }
            }
    }
    
  • AlainDiartAlainDiart FRMember ✭✭
    edited January 2018

    Thank you Mulflar, I thought about this and will fallback to this if I find no other solution, but Contact entity is used in many other entities (like tenth of occurrences, don't ask me why, the third party Api I'm using gives me that), I'd have love something more centralized than this. But thank you very much indeed to confirm my thoughts. :)

    It looks like the FallbackValue is what I'm looking for, but that it's not implemented in Xamarin...

  • eldeekeldeek Member ✭✭

    @Mulflar said:
    Mmm... something like this?

    namespace MyProject.Models
    {
    public class Intervention
    {
    [JsonProperty("id")]
    public int Id { get; set; }

          [JsonProperty("participant")]
          public Contact participant { get; set; }
              
          [JsonIgnore]
              public Contact Participant { 
              get
              {
                  if(participant!=null)
                      return participant
                  else
                      return new Contact { FirstName ="blank", LastName ="blank"};
              }
              set
              {
                  participant = value;
              }
          }
              }
      }
    

    I love it.

Sign In or Register to comment.