StringFormat in Xaml

NickANickA USMember ✭✭
edited February 11 in Xamarin.Forms

I'm trying to add addition text to a biding. It works fine as lone as I include "=". Here's an example:

<Label Text = "{Binding DueDate, StringFormat='Due={0}'}"   />

Displays:

Due=02/15/2019

That's not what I want it to display I want it to be "Due 05/15/2019", but when I try this:

<Label Text = "{Binding DueDate, StringFormat='Due {0}'}"   />

it only displays the word "Due" and that's it. The only way it works is when I add "=".

Yes, I can create a property or override tostring(), but this is how I want to implement it. I'm just curious as to why I need that "=" in there/

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 11

    Please show more your code , it works perfectly on my side .

  • NickANickA USMember ✭✭

    It's the oddest thing because I created a test project and everything worked fine. But in my main project, if there's a space between the text and the {0} placeholder, it won't show the value. Here's the code:

                var lst = (from db in context.BillsDue
                           .Include(p => p.Payee)
                           .Include(c => c.Category)
                           .Include(a => a.Account)
                           where db.IsDeleted == 0
                           select db).ToList();
    
                lstBills.ItemsSource = lst;
    

    This is using EF Core by the way. So BillsDue class has a property of type DateTime called DueDate.

    public DateTime DueDate { get; set; }

    lstBills is a ListView, and the Label is in a Grid in a ViewCell

                                        <Label FontSize="20"
                                           Text = "{Binding DueDate, StringFormat='Due on {0}'}" 
                                           Grid.Row="2" Grid.Column="0"  />
    

    The above line will only display "Due on", but if I take the space out after the word "on" it works, but of course I then don't have the space.

    So I don't know what's going and why it cares about the space.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 12

    I don't clear about this cause either, you could try to convert the type of DueDate ,just change it from DateTime to string.

  • NickANickA USMember ✭✭

    @ColeX said:
    I don't clear about this cause either, you could try to convert the type of DueDate ,just change it from DateTime to string.

    I can't. It's a DateTime field in a database. I don't want it stored as a string or have to convert it every time I want to use it.

    It's a bug in Xamarin. If I use a string type it works, but not with a DateTime. For some reason it doesn't like to have a blank space between the text and the {0}. Without a space it works find. Just another Xamarin Forms quirk to work around. I will file a bug report.

  • NMackayNMackay GBInsider, University mod

    @NickA said:

    @ColeX said:
    I don't clear about this cause either, you could try to convert the type of DueDate ,just change it from DateTime to string.

    I can't. It's a DateTime field in a database. I don't want it stored as a string or have to convert it every time I want to use it.

    It's a bug in Xamarin. If I use a string type it works, but not with a DateTime. For some reason it doesn't like to have a blank space between the text and the {0}. Without a space it works find. Just another Xamarin Forms quirk to work around. I will file a bug report.

    Please link it back to this post and post the github issue here, so many people say it a bug and then later realise it's a mistake.

  • NickANickA USMember ✭✭

    I wish it was a mistake. I already found a workaround though so I'm good. The Xamarin devs have enough to work on already, this is a minor issue.

  • NMackayNMackay GBInsider, University mod

    @NickA said:
    I wish it was a mistake. I already found a workaround though so I'm good. The Xamarin devs have enough to work on already, this is a minor issue.

    Please share the fix as it will help others.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    You mean you used the IValueConverter?

  • NickANickA USMember ✭✭

    Yep. I just return a simple string:

    var retVal = string.Format("Due: {0}", datetime.ToShortDateString());

Sign In or Register to comment.