Peter
Peter

Reputation: 38455

Silverlight databinding question

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?

Upvotes: 1

Views: 106

Answers (4)

funwithcoding
funwithcoding

Reputation: 11

You can definitely databind to properties.

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

Check this URL.

Upvotes: 1

Jordan
Jordan

Reputation: 558

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)

Upvotes: 3

Codism
Codism

Reputation: 6224

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.

Upvotes: 2

Pierreten
Pierreten

Reputation: 10147

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.

Upvotes: -2

Related Questions