Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Databinding to simple controls with RIA services

Databinding to simple controls with RIA services

Scheduled Pinned Locked Moved WPF
wpftutorialcsswcf
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RB Emphasys
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • R RB Emphasys

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      R 1 Reply Last reply
      0
      • M Mark Salsbery

        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:

        R Offline
        R Offline
        RB Emphasys
        wrote on last edited by
        #3

        Worked perfectly, thanks Mark.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups