Databinding to simple controls with RIA services
-
Guys, I'm just picking up silverlight and Ria services, and must admit I'm a little frusterated. I have a working example of querying via ria services and binding to complex controls such as data grids, but if I try to use that same logic on something like a textblock, I get no love. I assume it's the asyncrhonus nature of the call, but every single example I find uses a darn data grid. Here is what I'm doing: code behind:
SellerDomainContext domainContext = new SellerDomainContext(); SellerGrid.ItemsSource = domainContext.Sellers; FirstName.DataContext = domainContext.Sellers; LastName.DataContext = domainContext.Sellers; domainContext.Load(domainContext.GetSellerByUsernameQuery("joe\_seller"));
xaml:
<TextBox HorizontalAlignment="Left" Margin="8,8,0,19" Width="212" x:Name="FirstName" Text="{Binding TimeOfDay, Mode=OneWay}" /> <TextBox HorizontalAlignment="Left" Margin="8,8,0,19" Width="212" x:Name="LastName" Text="{Binding LastName, Mode=OneWay}" /> <data:DataGrid x:Name="SellerGrid"></data:DataGrid>
As I said, the grid works fine, the first and last name text blocks do not. Any links to articles or code snippets to instruct me on how to do simple data binding to objects such as a text block via ria services would be much appreciated! Thanks again, Ryan
-
Guys, I'm just picking up silverlight and Ria services, and must admit I'm a little frusterated. I have a working example of querying via ria services and binding to complex controls such as data grids, but if I try to use that same logic on something like a textblock, I get no love. I assume it's the asyncrhonus nature of the call, but every single example I find uses a darn data grid. Here is what I'm doing: code behind:
SellerDomainContext domainContext = new SellerDomainContext(); SellerGrid.ItemsSource = domainContext.Sellers; FirstName.DataContext = domainContext.Sellers; LastName.DataContext = domainContext.Sellers; domainContext.Load(domainContext.GetSellerByUsernameQuery("joe\_seller"));
xaml:
<TextBox HorizontalAlignment="Left" Margin="8,8,0,19" Width="212" x:Name="FirstName" Text="{Binding TimeOfDay, Mode=OneWay}" /> <TextBox HorizontalAlignment="Left" Margin="8,8,0,19" Width="212" x:Name="LastName" Text="{Binding LastName, Mode=OneWay}" /> <data:DataGrid x:Name="SellerGrid"></data:DataGrid>
As I said, the grid works fine, the first and last name text blocks do not. Any links to articles or code snippets to instruct me on how to do simple data binding to objects such as a text block via ria services would be much appreciated! Thanks again, Ryan
In the current RIA services, DomainContext queries ALWAYS return a collection of entities, not a single entity. A collection will not have the "LastName" property on it... that's a property of your entity class (I'd assume :)) First, if you haven't already, upgrade to the latest WCF RIA services (here: WCF RIA Services[^]) Then your code behind could be changed to something like this:
using System.Linq; // needed for Entities.FirstOrDefault()!
...
SellerDomainContext domainContext = new SellerDomainContext(); domainContext.Load(domainContext.GetSellerByUsernameQuery("joe\_seller"), getSellerByUsernameLoadOp\_Completed, null);
...
void getSellerByUsernameLoadOp\_Completed(LoadOperation loadOperation) { if (loadOperation.HasError) { loadOperation.MarkErrorAsHandled(); // do some error handling here } else { SellerGrid.ItemsSource = loadOperation.Entities; FirstName.DataContext = loadOperation.Entities.FirstOrDefault(); LastName.DataContext = loadOperation.Entities.FirstOrDefault(); } }
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
In the current RIA services, DomainContext queries ALWAYS return a collection of entities, not a single entity. A collection will not have the "LastName" property on it... that's a property of your entity class (I'd assume :)) First, if you haven't already, upgrade to the latest WCF RIA services (here: WCF RIA Services[^]) Then your code behind could be changed to something like this:
using System.Linq; // needed for Entities.FirstOrDefault()!
...
SellerDomainContext domainContext = new SellerDomainContext(); domainContext.Load(domainContext.GetSellerByUsernameQuery("joe\_seller"), getSellerByUsernameLoadOp\_Completed, null);
...
void getSellerByUsernameLoadOp\_Completed(LoadOperation loadOperation) { if (loadOperation.HasError) { loadOperation.MarkErrorAsHandled(); // do some error handling here } else { SellerGrid.ItemsSource = loadOperation.Entities; FirstName.DataContext = loadOperation.Entities.FirstOrDefault(); LastName.DataContext = loadOperation.Entities.FirstOrDefault(); } }
Mark Salsbery Microsoft MVP - Visual C++ :java:
Worked perfectly, thanks Mark.