views:

63

answers:

4

Let's say I have a Class called ModelBase

public class ModelBase
{
  public string Name
  {
     get { return "one"; }
  }
}

and I have a property named Model of type ModelBase.

Now to the question how do I Bind to the Name property? The c# code would be this.Model.Name.

I've been trying to get this to work a long time, can some one enlighten me?

A: 

Silverlight doesn't allow binding to properties. You'll need to expose a property on your viewmodel that returns the value of the models properties to bind correctly.

Pierreten
Sorry mate, ur wrong there. Silverlight definitely does allow binding to properties!! If you want more control and response you may need to set these up as notification properties or dependency properties, but they are all supported...
Mark Cooper
+3  A: 

Not sure why you are having trouble with this.

You should be able to set the object that the Model property is on as the DataContext for your control, then simply bind using {Binding Model.Name}...

What have you tried to do so far?

(You can definitely bind to properties in Silverlight BTW)

Jordan
+1  A: 

You can definitely databind to properties.

If you want more, you can use dependency properties of silverlight.

Check this URL.

funwithcoding
+2  A: 

You need to assign Model to the datacontext property before you can do any data binding, an example would be:

this.DataContext = Model;

In xaml, setup binding in this way:

<TextBlock Text={Binding Name}/>

Note: The way you declare the Name property only allows one time binding, to allow OneWay/TwoWay binding, look at dependencyproperty or INotifyPropertyChanged interface.

Codism
You can also do <TextBlock Text="{Binding YourDataContext.Property.SubProperty.SubSubProperty.etc}" /> which might suit your needs slightly better?
Mark Cooper