Print Page | Close Window

Property names

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2452
Printed Date: 14-Sep-2025 at 2:10am


Topic: Property names
Posted By: tj62
Subject: Property names
Date Posted: 20-Jan-2011 at 4:06am
Hi,
One of the weaknesses in XAML and Silverlight is when you are doing data binding, where you refer to a bound property by the property name in the XAML. If you do a typo in the XAML or change the property name in the C# code forgetting to update it in the XAML code you get no binding.

Is there any method of avoiding this for example by static resources?

A related question is regarding the OnPropertyChanged event. Example below where overriding that event handler in an Entity class:

        protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)

        {

            base.OnPropertyChanged(e);

            if(e.PropertyName == "p_tsID" || e.PropertyName == "p_ts")

            {

// Do some stuff

            }

        }

In the above example can I somehow get the property names by code instead of using hard-coded strings ("p_tsID", "p_ts")? That would eliminate the risk if I later decide to change the name of those properties in the entity model and forget to update those strings accordingly.
You have this possibility in the EntitySet's via the EntitySetName property. But I can't find how to access that for Entity-Properties.




Replies:
Posted By: tj62
Date Posted: 20-Jan-2011 at 7:18am
Well found the answere to my later question so forget that one:
EntItem.EntityPropertyNames.p_tsID  returns "p_tsID" and so on.

The first question is still open.


Posted By: DenisK
Date Posted: 21-Jan-2011 at 10:53am
Hi tj62;

There are several options that I know of to minimize the error of binding with a property that doesn't exist:

1. When debugging, the debug output window should show something like this in the event of binding error.

System.Windows.Data Error: BindingExpression path error: 'Outputxxxxx' property not found on 'SilverlightApplication1.MainPageViewModel' 'SilverlightApplication1.MainPageViewModel' (HashCode=53180767). BindingExpression: Path='Outputxxxxx' DataItem='SilverlightApplication1.MainPageViewModel' (HashCode=53180767); target element is 'System.Windows.Controls.TextBox' (Name='txtOutput'); target property is 'Text' (type 'System.String')..

2. You can use a Fallback value markup as a default value in case a binding error occurs.

<TextBox Name="txtOutput"
                 Text="{Binding Outputxxxxx, FallbackValue='Binding has failed'}"

3. By googling this, I was able to find other options that other developers seem to use. One of them that I think is more helpful than the rest is this.

http://karlshifflett.wordpress.com/2009/06/08/glimpse-for-silverlight-viewing-exceptions-and-binding-errors/ - http://karlshifflett.wordpress.com/2009/06/08/glimpse-for-silverlight-viewing-exceptions-and-binding-errors/

Hope this helps.


Posted By: tj62
Date Posted: 22-Jan-2011 at 3:39pm
Thank you DenisK, this gives me some idas.



Print Page | Close Window