views:

65

answers:

3

I have silverlight usercontrol. This contains Service Entity object. see below

public partial class MainPage : UserControl
{
   public ServiceRef.tPage CurrentPage { get; set; }
...
}

I need to bind CurrentPage.Title to TextBox

My xaml is here

<TextBox Text="{Binding Path=CurrentPage.Title, RelativeSource={RelativeSource self}}"></TextBox>

But it is not work.

How to do it?

+2  A: 

In order for that to work, you'll have to implement INotifyPropertyChanged on your class and raise the PropertyChanged event for CurrentPage when it's set (this also means you won't be able to use auto properties; you'll have to use your own private instance backing variable and code the get { } and set { } yourself).

What's happening is the control is binding to the value before you've set CurrentPage. Because you aren't notifying anyone that the property has changed, it does not know to refresh the bound data. Implementing INotifyPropertyChanged will fix this.

Or you could just manually set the Text property yourself in the setter.

Adam Robinson
Thereupon I must implement Entity class (tPage) from `INotifyPropertyChanged` interface. That's True?If my understand is true, How to implement all entity class automatically? Because entity classes generated automatically
ebattulga
@ebattulga: If by "Entity" you mean an ADO.NET Entity Framework entity, then they already implement this interface.
Adam Robinson
A: 

Change your markup to

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=CurrentPage.Title}" />

By assigning RelativeSource={RelativeSource self} your are telling the TextBlock to bind to itself and look for a property named CurrentPage on the TextBlock itself and not the parent Window.

decyclone
A: 

set the UpdateSourceTrigger="PropertyChanged" in the XAML.

Avatar