Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Databinding to Classes Inside Classes
4 replies. Latest Post by dotnutshell on July 21, 2008.
(0)
shaggygi
Member
59 points
88 Posts
04-28-2008 6:36 PM |
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
Contributor
2610 points
380 Posts
04-29-2008 1:56 AM |
The Binding should be like this: <TextBlock x:Name="tb1" Text="{Binding Time.Minutes}"/>
The Binding should be like this:
<
Set the data context:
period.Time = time;
tb1.DataContext = period;
The classes:
public class Time
Hope this helps.
Jim (http://jimmangaly.blogspot.com/)
Please mark the replies as answers if they answered your question
04-29-2008 5:47 PM |
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.
04-30-2008 12:49 AM |
There are two ways you can do this:
First, the hack (if you haven't already thought of it ):
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
dotnutshell
118 points
64 Posts
07-21-2008 8:43 AM |
http://www.softwaredevelopmentexperts.net/programming_-_silverlight_-_data_binding.show.73.aspx