Skip to main content

Microsoft Silverlight

Answered Question Databinding to Classes Inside ClassesRSS Feed

(0)

shaggygi
shaggygi

Member

Member

59 points

88 Posts

Databinding to Classes Inside Classes

How do you databind to class variables inside of another class variable.  Meaning...

string Time;

If I have a class and instaniate a variable, I can databind it to a control property ( TextBlock Text="{Binding Time}" ).  Works fine.

 class Time

{

   string Minutes;

   string Seconds;

}

How would you do this for Minutes???   ( TextBlock Text="{Binding Time.Minutes???}" )

 

Jim Mangaly
Jim Mangaly

Contributor

Contributor

2610 points

380 Posts

Answered Question

Re: Databinding to Classes Inside Classes

The Binding should be like this:

<TextBlock x:Name="tb1" Text="{Binding Time.Minutes}"/>

 

Set the data context:

Period period = new Period();

Time time = new Time();

time.Minutes = "55";

period.Time = time;

tb1.DataContext = period;

 

 The classes:

public class Period

{

    private Time _time;
    public Time Time

    {

     get

     {

      return _time;

     }

    set

    {

     _time =
value;

    }

  }

}

 

public class Time

{

  private string _minutes;  public string Minutes

  {

   get

   {

     return _minutes;

     }

    set

    {

     _minutes =
value;

     }

  }

Hope this helps.

Jim (http://jimmangaly.blogspot.com/)

Please mark the replies as answers if they answered your question

http://www.identitymine.com/

shaggygi
shaggygi

Member

Member

59 points

88 Posts

Re: Databinding to Classes Inside Classes

Thanks for the reply.  Now for another question...

Can you mix text with data that it is binding with?  Meaning,  

<TextBlock x:Name="tb1" Text="{Binding Time.Minutes} Mins."/> will return 55 Mins.

Is it possible to bind a control property to more than one item?  Meaning,

<TextBlock x:Name="tb1" Text="{Binding Time.Minutes Mins. Time.Seconds Secs.} Mins"/> will return 55 Mins. 0 Secs.

Jim Mangaly
Jim Mangaly

Contributor

Contributor

2610 points

380 Posts

Answered Question

Re: Databinding to Classes Inside Classes

There are two ways you can do this:

First, the hack (if you haven't already thought of it Smile):

<StackPanel>

  <TextBlock x:Name="tb1" Text="{Binding Time.Minutes}"/>

  <TextBlock Text=" Mins."/>

</StackPanel>

This could be a simple solution in your case. I know this is not always a solution.

The other solution is to use a converter. Pass your source to the converter. In the converter's Convert() method write the logic to find the minutes and seconds, add whatever text you want to add (like 'minutes' or 'secs'), and finally return the converted value to the binding. See this thread for more details on how to implement a converter: http://silverlight.net/forums/p/14724/48854.aspx

Hope this helps.

Jim (http://jimmangaly.blogspot.com/)

 Please mark the replies as answers if they answered your question

 

 

http://www.identitymine.com/
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities