New Posts New Posts RSS Feed: Property names
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Property names

 Post Reply Post Reply
Author
tj62 View Drop Down
Groupie
Groupie
Avatar

Joined: 21-May-2009
Location: Iceland
Posts: 81
Post Options Post Options   Quote tj62 Quote  Post ReplyReply Direct Link To This Post Topic: Property names
    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.

Back to Top
tj62 View Drop Down
Groupie
Groupie
Avatar

Joined: 21-May-2009
Location: Iceland
Posts: 81
Post Options Post Options   Quote tj62 Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.


Hope this helps.
Back to Top
tj62 View Drop Down
Groupie
Groupie
Avatar

Joined: 21-May-2009
Location: Iceland
Posts: 81
Post Options Post Options   Quote tj62 Quote  Post ReplyReply Direct Link To This Post Posted: 22-Jan-2011 at 3:39pm
Thank you DenisK, this gives me some idas.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down