<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Adam Kinney posts tagged with 'databinding'</title><description>Adam Kinney blog posts filtered by a specific tag</description><link>http://adamkinney.com/blog/tags/databinding/default.aspx</link><language>en-us</language><pubDate>Sat, 19 Jul 2008 18:58:22 GMT</pubDate><generator>Oxite</generator><item><title>Building the Gamercard Part 3, Updating the UI</title><description>&lt;p&gt;This is Part 3 in a series covering how the &lt;a href="http://adamkinney.com/blog/322/default.aspx"&gt;Xbox Gamercard&lt;/a&gt; application was made using Silverlight 2.&amp;#160; In this part I will cover how the interface is updated once the data is retrieved.&lt;/p&gt;  &lt;p&gt;In the first part of the series I showed a few highlights of how the UI was created.&amp;#160; In the second part, I showed the call to the web service and how the results were parsed using LINQ to create a .NET object.&amp;#160; In this part, I will show how an instance of the custom &lt;em&gt;XboxInfo&lt;/em&gt; class is used as the data source for Data Binding the UI Elements.&lt;/p&gt;  &lt;h2&gt;Data Binding&lt;/h2&gt;  &lt;p&gt;Returning to the parsing code from &lt;a href="http://adamkinney.com/blog/326/default.aspx"&gt;part 2&lt;/a&gt;, the web service results are returned as a string which is handed to a parsing method.&amp;#160; Within the method an &lt;em&gt;XDocument&lt;/em&gt; is created that a LINQ query is run against that returns an &lt;em&gt;XboxInfo&lt;/em&gt; class instance.&amp;#160; The DataContext is then set to the instance and this is where the Data binding magic begins.&lt;/p&gt; &lt;code&gt;XDocument xDoc = XDocument.Parse(xmlContent);    &lt;br /&gt;XboxInfo gamer = ...//LINQ query     &lt;br /&gt;this.DataContext = gamer;&lt;/code&gt;   &lt;p&gt;In the above code, &lt;em&gt;this&lt;/em&gt; is referring to an instance of my XboxGamerCard.Page class which is the UserControl used as the main or RootVisual control of the application.&amp;#160; This class is where the main UI is declared in XAML including the main Grid control from &lt;a href="http://adamkinney.com/blog/325/default.aspx"&gt;Part 1&lt;/a&gt;.&amp;#160; Rather than writing the familiar manual &lt;em&gt;lnkGamerTag.Text = gamer.GamerTag &lt;/em&gt;type code we can use Data Binding to update the UI.&lt;/p&gt;  &lt;p&gt;A simple example of the binding is shown below.&amp;#160; The Content property of the HyperlinkButton is set to a GamerTag property of the DataContext.&amp;#160; Once the DataContext of the UserControl is set, the Bindings are notified and the values are updated.&amp;#160; In the case of the Binding below, the DataContext is checked for a GamerTag property.&amp;#160; If it exists the value of the HyperlinkButton.Content property is set automatically to the GamerTag value.&amp;#160; If the DataContext GamerTag property were to change again, the Content property would be updated as well.&amp;#160; The target property and the data source are now connected via the Binding instance.&lt;/p&gt;  &lt;div style="float: right; margin-bottom: 10px; margin-left: 10px"&gt;&lt;a href="http://adamkinney.com/blog/322/default.aspx"&gt;&lt;img src="http://adamkinney.com/studios/xboxfriendswatch/images/xbf_gamercard.png" /&gt;&lt;/a&gt; &lt;/div&gt;  &lt;p&gt;&lt;code&gt;&amp;lt;HyperlinkButton Content=&amp;quot;{Binding GamerTag}&amp;quot; ... /&amp;gt;&lt;/code&gt; &lt;/p&gt;  &lt;p&gt;Another interesting feature of Data Binding is converting values.&amp;#160; The screenshot of the Gamercard to the right shows four green lights indicating that the status of this Quemark person is online rather than offline, which is indicated by a single red light.&lt;/p&gt;  &lt;p&gt;Using the Binding syntax below, the Canvas.Visibility property is bound to the StatusText property and a custom converter is set.&amp;#160;&amp;#160; Notice in the syntax, that StatusText is preceded by PresenceInfo.&amp;#160; A property of the XboxInfo class, PresenceInfo is a custom class as well with a string property titled StatusText.&amp;#160; With the &amp;quot;.&amp;quot; notation you can navigate the property chain.&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&amp;lt;Canvas Visibility=&amp;quot;{Binding PresenceInfo.StatusText,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/code&gt;&lt;code&gt;Converter={StaticResource StatusVisiblityConverter}}&amp;quot;&amp;gt;...&amp;lt;/Canvas&amp;gt;&lt;/code&gt; &lt;/p&gt;  &lt;p&gt;The StatusText property is a string but the Visibility property is an enum of type System.Windows.Visibility.&amp;#160; By implementing the &lt;em&gt;IValueConverter &lt;/em&gt;the custom converter class can be used to check a string value and return a Visibility enum value.&amp;#160; The code is simple to write and looks like the following:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;public class StatusVisiblity : IValueConverter      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public object Convert(object value, Type targetType, object parameter, CultureInfo culture)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string statusText = (string)value;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (statusText == &amp;quot;Offline&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return Visibility.Collapsed;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return Visibility.Visible;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160; &lt;br /&gt;}&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;One other interesting area is RecentGames, a property of the XboxInfo class of type List&amp;lt;XboxUserGameInfo&amp;gt;.&amp;#160; In order to show this collection, The RecentGames property is used as the datasource for an ItemsSource property of ItemsControl added to the page.&amp;#160; When bound, each item in the collection is shown using the the defined ItemTemplate where Binding are defined using the properties of the XboxUserGameInfo class.&lt;/p&gt; &lt;code&gt;&amp;lt;ItemsControl ItemsSource=&amp;quot;{Binding RecentGames}&amp;quot; ...&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemsControl.ItemTemplate&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;DataTemplate&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Grid ...&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Rectangle ... /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Image Width=&amp;quot;32&amp;quot; Height=&amp;quot;32&amp;quot; Source=&amp;quot;{Binding Game.Image32Url}&amp;quot;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Image&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Grid&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/DataTemplate&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemsControl.ItemTemplate&amp;gt;     &lt;br /&gt;&amp;lt;/ItemsControl&amp;gt;&lt;/code&gt;   &lt;p&gt;Databinding can be very useful and is definitely worth looking more into.&amp;#160; Not only does it save the developer some lines of code, it can also be useful during design-time due to the Data Binding support within Expression Blend.&amp;#160; That sounds like another blog post in the works...&lt;/p&gt;  &lt;p&gt;For more information on Data Binding check out Jesse Liberty's &lt;a href="http://silverlight.net/learn/tutorials/databinding.aspx"&gt;Data Binding Tutorial&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Refreshing the Data&lt;/h2&gt;  &lt;div style="float: right; margin-bottom: 10px; margin-left: 10px"&gt;&lt;a href="http://adamkinney.com/studios/xboxfriendswatch/gamercard.html"&gt;&lt;img src="http://adamkinney.com/images/xbfgc/scriptpage.jpg" /&gt;&lt;/a&gt; &lt;/div&gt;  &lt;p&gt;The last piece I am going to talk about covers browser integration.&amp;#160; The &lt;a href="http://adamkinney.com/studios/xboxfriendswatch/gamercard.html"&gt;Gamercard Script Builder&lt;/a&gt; page (pictured to the right), has a textbox where you enter your Gamertag and when you TAB out of it the embed script is added to the textarea below.&amp;#160; In addition the Gamercard application to the right is updated and you see a preview of what your card will look like.&lt;/p&gt;  &lt;p&gt;Due to the awesome browser integration available in Silverlight 2 this was very simple to do.&amp;#160; The first step is to expose the &lt;em&gt;RequestGamerXml&lt;/em&gt; method (explained in &lt;a href="http://adamkinney.com/blog/326/default.aspx"&gt;Part 2&lt;/a&gt;) to JavaScript which is done by adding an attribute to the public method and registering the class instance as a ScriptableObject.&lt;/p&gt; &lt;code&gt;   &lt;p&gt;[ScriptableMember]      &lt;br /&gt;public void RequestGamerXml(string gamerTag){...}&lt;/p&gt;    &lt;p&gt;void Page_Loaded(object sender, RoutedEventArgs e)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; HtmlPage.RegisterScriptableObject(&amp;quot;GamerCard&amp;quot;, this);       &lt;br /&gt;}&lt;/p&gt; &lt;/code&gt;  &lt;p&gt;Then in the JavaScript function call into that object and pass a variable to the exposed method.&amp;#160; &lt;em&gt;slPlugIn&lt;/em&gt; is the id of the object tag used to instantiate the Silverlight application.&lt;/p&gt; &lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;    &lt;br /&gt;function genScript(){     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; document.getElementById(&amp;quot;slPlugin&amp;quot;).content.GamerCard.RequestGamerXml(tagEntered);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;}     &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/code&gt;   &lt;p&gt;And that's it!&amp;#160; Now every time the Gamertag entered changes, the RequestGamerXml method is called and the DataContext is reset.&amp;#160; Thanks to the Bindings in place the interface Elements are updated to stay in synch with the data source.&lt;/p&gt;  &lt;p&gt;For more information on browser integration try the &lt;a href="http://download.microsoft.com/download/9/4/e/94e080c7-d462-4118-b07a-55578d64bc43/Silverlight%202%20Beta%201%20-%20Browser%20Integration.zip"&gt;Browser Integration Hands-on Lab&lt;/a&gt; from MIX08.&lt;/p&gt;  &lt;h2&gt;Conclusion&lt;/h2&gt;  &lt;p&gt;This was a fun project and the ideas keep on coming.&amp;#160; Expect to see more in the future from the &lt;a href="http://adamkinney.com/studios/xboxfriendswatch/"&gt;Xbox Friends Watch Lab&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Other parts in this series are available:&lt;/p&gt;  &lt;li&gt;&lt;a href="http://adamkinney.com/blog/325/default.aspx"&gt;Building the Gamercard Part 1, Composing the UI&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://adamkinney.com/blog/326/default.aspx"&gt;Building the Gamercard Part 2, Retrieving the Data&lt;/a&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;img style="border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none" alt="my diigo links" src="http://www.diigo.com/images/ii_blue.gif" /&gt;&amp;#160;&lt;a title="more info on Silverlight controls" href="http://www.diigo.com/user/adamkinney/silverlight+databinding"&gt;silverlight+databinding&lt;/a&gt;, &lt;a title="more info on Silverlight controls" href="http://www.diigo.com/user/adamkinney/silverlight"&gt;silverlight&lt;/a&gt;&lt;/p&gt; &lt;/li&gt;&lt;img src="http://adamkinney.com/blog/327/aggviewbug/default.aspx" alt="" /&gt;</description><comments>http://adamkinney.com/blog/327/default.aspx</comments><link>http://adamkinney.com/blog/327/default.aspx</link><pubDate>Thu, 17 Apr 2008 23:41:00 GMT</pubDate><guid isPermaLink="true">http://adamkinney.com/blog/327/default.aspx</guid><dc:creator>Adam Kinney</dc:creator><slash:comments>0</slash:comments><trackback:ping>http://adamkinney.com/blog/327/trackback/default.aspx</trackback:ping><category>databinding</category><category>Silverlight</category><category>XboxFriendsWatch</category></item></channel></rss>