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. Binding to a property of the page in XAML

Binding to a property of the page in XAML

Scheduled Pinned Locked Moved WPF
wpfwcfquestion
5 Posts 3 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.
  • G Offline
    G Offline
    gantww
    wrote on last edited by
    #1

    Hello all, I have a small bit of silverlight code that pulls some data down from a silverlight-enabled web service and binds to it. I'm currently doing the bulk of the work in the constructor. Once the data is down, I bind to it in a couple of places and it is also accessible via a property on the code behind. However, I also have some controls that need to bind to this property in order to provide a list of values on screen. Is there a way to do this with the XAML binding syntax, or do I need to do this in the code behind? Thanks, Will

    M 1 Reply Last reply
    0
    • G gantww

      Hello all, I have a small bit of silverlight code that pulls some data down from a silverlight-enabled web service and binds to it. I'm currently doing the bulk of the work in the constructor. Once the data is down, I bind to it in a couple of places and it is also accessible via a property on the code behind. However, I also have some controls that need to bind to this property in order to provide a list of values on screen. Is there a way to do this with the XAML binding syntax, or do I need to do this in the code behind? Thanks, Will

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      What about this? For example:

      <UserControl ...
      xmlns:model=".........."

      <UserControl.DataContext>
      model:YourClass/
      <UserControl.DataContext>

      <ListBox ItemSource="{Binding YourPropertyOfYourClass}" />

      </UserControl>

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders

      G 2 Replies Last reply
      0
      • M Michael Sync

        What about this? For example:

        <UserControl ...
        xmlns:model=".........."

        <UserControl.DataContext>
        model:YourClass/
        <UserControl.DataContext>

        <ListBox ItemSource="{Binding YourPropertyOfYourClass}" />

        </UserControl>

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders

        G Offline
        G Offline
        gantww
        wrote on last edited by
        #3

        Sorry about the slow response, it took longer to recover from vacation than I thought (I had to wait until I was off to have time to start learning silverlight). Your suggestion worked, so that means I don't understand the whole DataContext thing like I thought I did. I thought the control got it from whatever control contained it, but that is apparently not the case. Strange. Anyway, thanks for the help, I really appreciate it.

        1 Reply Last reply
        0
        • M Michael Sync

          What about this? For example:

          <UserControl ...
          xmlns:model=".........."

          <UserControl.DataContext>
          model:YourClass/
          <UserControl.DataContext>

          <ListBox ItemSource="{Binding YourPropertyOfYourClass}" />

          </UserControl>

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders

          G Offline
          G Offline
          gantww
          wrote on last edited by
          #4

          Ugh. I told you wrong. I forgot I had put some hackish stuff in the code behind that set the ItemsSource. Here's roughly what I have. It's buried down in one cell of a datagrid that is in a cell of another datagrid on the page. <ComboBox x:Name="cboTextColor" SelectedItem="{Binding Color}" Grid.Row="2" Grid.Column="1"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Color}" Width="25"/> <TextBlock Text="{Binding ColorName}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> There is a regular .NET property on the code behind page called ProductColors. It is currently an ObservableCollection (where ColorEntry has an id, a color code, and a description). It seems like I should be able to bind to it by doing something like: <ComboBox ItemsSource={Binding ProductColors}" /> However, even though the list has two items in it, nothing shows in the drop down. Does this need to be a dependency property or something for binding to work?

          M 1 Reply Last reply
          0
          • G gantww

            Ugh. I told you wrong. I forgot I had put some hackish stuff in the code behind that set the ItemsSource. Here's roughly what I have. It's buried down in one cell of a datagrid that is in a cell of another datagrid on the page. <ComboBox x:Name="cboTextColor" SelectedItem="{Binding Color}" Grid.Row="2" Grid.Column="1"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Color}" Width="25"/> <TextBlock Text="{Binding ColorName}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> There is a regular .NET property on the code behind page called ProductColors. It is currently an ObservableCollection (where ColorEntry has an id, a color code, and a description). It seems like I should be able to bind to it by doing something like: <ComboBox ItemsSource={Binding ProductColors}" /> However, even though the list has two items in it, nothing shows in the drop down. Does this need to be a dependency property or something for binding to work?

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

            gantww wrote:

            It seems like I should be able to bind to it by doing something like:

            That depends on what "ProductColors" is. Assuming it's a collection of ColorEntry objects, you still need a data context set somewhere so the system knows where to find the ProductColors object to bind to.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            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