views:

409

answers:

3

I understand that Silverlight 3.0 has binding but just want a simple example on how to use this to read a property from a class.
I have a class called Appointment which as a String property called Location:

    Public Property Location() As String
        Get
            Return _Location
        End Get
        Set(ByVal Value As String)
            _Location = Value
        End Set
    End Property

With a Private Declaration for the _Location as String of course.
I want a XAML element to bind to this property to display this in a TextElement, but it must be in XAML and not code, for example I want something like this:

<TextBlock Text="{Binding Appointment.Location}"/>

What do I need to do to get this to work?
It has to be a Silverlight 3.0 solution as some WPF features are not present such as DynamicResource which is what I'm used to using.


Just to add that my XAML is being loaded in from a seperate XAML File, this may be a factor in why the binding examples don't seem to work, as there are different XAML files the same Appointment.Location data needs to be applied.

A: 

If I'm reading correctly, you need to create an instance of Appointment, set the DataContext of the control to that instance and modify your binding to just say: Text="{Binding Location}"

Also, consider implementing INotifyPropertyChanged on your Appointment class to allow the data classes to notify the UI of property value changes.

James Cadd
That sounds like what I need - how to I set the DataContext in XAML to the Instance of the Class?
RoguePlanetoid
+3  A: 

You have two options.

If the "Appointment" class can be used as the DataContext for the control or Window, you can do:

<TextBlock Text="{Binding Location}" />

If, however, "Appointment" is a property of your current DataContext, you need a more complex path for the binding:

<TextBlock Text="{Binding Path=Appointment.Location}" />

Full details are documented in MSDN under the Binding Declarations page. If neither of these are working, make sure you have the DataContext set correctly.

Reed Copsey
Thanks for this at least I have something that works now, got used to DynamicResources but this is more powerful and also a little neater in my XAML
RoguePlanetoid
+1  A: 

You need something in code, unless you want to declare an instance of Appointment in a resource and bind to that but I doubt thats what you want.

You need to bind the Text property to the Property Path "Location" then assign the DataContext of the containing XAML to an instance of the Appointment:-

<Grid x:Name="LayoutRoot" Background="White">
   <TextBlock Text="{Binding Location}" />
</Grid>

Then in the control's load event:-

 void Page_Loaded(object sender, RoutedEventArgs e)
 {
    this.DataContext = new Appointment() { Location = "SomePlace" };
 }

Note in this case I'm using the default Page control.

AnthonyWJones
Actually this made me realise something as that I am assigning the DataContext before the class is Instanced, if I assign the Appointment Class as a DataContext after my XAML is loaded it works!
RoguePlanetoid