Question

Jan Van der Haegen on Sat, 15 Feb 2014 16:17:18


Hey experts!

I'm creating a custom collection (grid) control for the Desktop client.  In my control, I can do two-way binding to the different properties on the 'pet' entity with bindings as "Color" or "Owner.ContactInfo.EmailAddress".

However, this seems to be binding to the raw 'value' underneath, and for Date, DateTime, Money, Phone Number, etc, all formatting is lost.

Is there anyway to use the LightSwitch formatting system to convert these raw values to their formatted counterparts?  

(I seem to have found it's possible to declare bindings as "Owner.ContactInfo.Details.Properties.PhoneNumber.FormattedValue", but that doesn't seem to work.)

Thanks!

Jan


It's your story - time to switch on the innovation.||About me||LightSwitch blog


Sponsored



Replies

Yanjin Xu - MSFT on Tue, 18 Feb 2014 03:25:38


Hi Jan

Do you mean that you want to format the value manually according to the value generated? First you could check whether the custom Controls and data binding in the LightSwitch HTML Client is fine.

Perhaps the value formatting methods are also available for you,

Generally we can modify the display format for numbers and dates in Visual Studio LightSwitch by setting the Format Pattern property.  By using the Format Pattern property, we can specify how numeric and date types appear in an application that you create in Visual Studio LightSwitch. 

To Format Values in LightSwitch,  we can also specify a formatting pattern for certain fields using the entity designer.  Specifying this in one place will make every screen that displays that field use the same formatting pattern.

hope it helps.

Regards

Jan Van der Haegen on Tue, 18 Feb 2014 14:59:41


Hey Angie,

thanks, but I'm one step further than the link you shared. I am formatting the values as per the link you shared in my LightSwitch app.  

Let me rephrase my question to focus on the problem a bit more: I have an entity called "Person" with a property "ContactNumber" of type "Phone Number".  When I display this property in the UI, it is properly formatted as 1-(985) 678 - 5406.  However, when I access the property from code (C# in the desktop client) like this:

var phone = myPerson.ContactNumber;

I am left with a simple, unformatted string: "19856785406".  How do I get the specified formatting to kick in?

Thanks!!

Jan

Justin Anderson on Wed, 05 Mar 2014 23:21:40


There is no way provided to get the formatted value of a phone number typed property. You would have to parse the value and format it yourself.

Jan Van der Haegen on Thu, 06 Mar 2014 01:12:52


Interesting...

Ok so there's no way provided to get the properly formatted value for any property, which is a bit of a bummer.  On a case by case bases, it seems you can... euhm... 'guess' how LightSwitch does it internally, then 'borrow' it's implementation.

            string testNumber = "4079856750"; 
            string formats = "C (AAA) NNN-NNNN;C (AAA) NNNNNNN;CAAANNNNNNN;(AAA) NNN-NNNN;(AAA) NNNNNNN;AAANNNNNNN;NNN-NNNN;";
            IEnumerable<string> formatz = formats.Split(new []{';'}, StringSplitOptions.RemoveEmptyEntries).ToList(); 
            string result = string.Empty; 

            var type = typeof(PhoneNumberViewerControl).Assembly.GetType("Microsoft.LightSwitch.Extensions.Validation.Internal.PhoneNumberValidation", true); 
            var mInfoMethod = type.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
                .First(m => m.Name == "TryFormatPhoneNumber" && m.GetParameters().Count() == 3);   
            var parameters = new object[] { testNumber, formatz, result };


            bool isParsed = (bool)mInfoMethod.Invoke("ThisArgumentIsIgnoredBecauseTheMethodIsStatic", parameters);
            if (isParsed)
            {
                result = (string)parameters[2]; //Silly out parameters.
            }


One would have to find out the correct formats though, which is done through the modelservice.

                        var att = model.Attributes.OfType<Microsoft.LightSwitch.Model.Storage.Attribute>().FirstOrDefault(
                            a =>
                                a.Class == "Microsoft.LightSwitch.Extensions:@PhoneNumberValidation");
                        if (att == null)
                            formatString = "C (AAA) NNN-NNNN;C (AAA) NNNNNNN;CAAANNNNNNN;(AAA) NNN-NNNN;(AAA) NNNNNNN;AAANNNNNNN;NNN-NNNN;";
                        else
                            formatString = att.Properties[0].Value;


So, with enough effort, one can make it work I guess.  

Random observation: isn't it weird that you store the formatting information with the model, and have a property .FormattedValue on your models, yet leave the responsibility of the formatting in the hands of the Controls?  

Anyways, thanks for confirming there's no supported way, and that you have to find a workaround for each 'type' of property...

Keep rocking LS!

Jan