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. WCF and WF
  4. Binding a ComboBox's ItemSource when in an ItemsControl DataTemplate - best way?

Binding a ComboBox's ItemSource when in an ItemsControl DataTemplate - best way?

Scheduled Pinned Locked Moved WCF and WF
csswpfwcfquestion
4 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.
  • J Offline
    J Offline
    James J Foster
    wrote on last edited by
    #1

    I have an ItemsControl bound to a collection within my view model, and a data template to display a data-entry form. Any suggestions on how I should bind the combobox to a property on the original data context? Because the ComboBox is part of the DataTemplate in an ItemTemplate, the context is set to the current item. Code Excerpt: <ItemsControl ItemsSource="{Binding Source=WorkingCopy.CalendarBasedAttributes.Attributes}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="5 20 0 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Label Content="{Binding CalendarTimePeriod}" Grid.Column="0" Grid.Row="0"/> <ComboBox ItemsSource="{Binding WHATGOESHERE?}" Grid.Column="1" Grid.Row="0" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate>

    M 1 Reply Last reply
    0
    • J James J Foster

      I have an ItemsControl bound to a collection within my view model, and a data template to display a data-entry form. Any suggestions on how I should bind the combobox to a property on the original data context? Because the ComboBox is part of the DataTemplate in an ItemTemplate, the context is set to the current item. Code Excerpt: <ItemsControl ItemsSource="{Binding Source=WorkingCopy.CalendarBasedAttributes.Attributes}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="5 20 0 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Label Content="{Binding CalendarTimePeriod}" Grid.Column="0" Grid.Row="0"/> <ComboBox ItemsSource="{Binding WHATGOESHERE?}" Grid.Column="1" Grid.Row="0" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate>

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

      James J. Foster wrote:

      WHATGOESHERE?

      Can you post an example of the class you are binding to (the type of objects in the WorkingCopy.CalendarBasedAttributes.Attributes collection)? All I can glean from your posted code is there's a "CalendarTimePeriod" member somewhere :) Mark

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

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        James J. Foster wrote:

        WHATGOESHERE?

        Can you post an example of the class you are binding to (the type of objects in the WorkingCopy.CalendarBasedAttributes.Attributes collection)? All I can glean from your posted code is there's a "CalendarTimePeriod" member somewhere :) Mark

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

        J Offline
        J Offline
        James J Foster
        wrote on last edited by
        #3

        I wanted to minimize my dependencies to within the ItemsControl, and not assume a particular relationship between objects in my ViewModel (the data context). Here's at least one solution that works:

        ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Stores}"

        M 1 Reply Last reply
        0
        • J James J Foster

          I wanted to minimize my dependencies to within the ItemsControl, and not assume a particular relationship between objects in my ViewModel (the data context). Here's at least one solution that works:

          ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Stores}"

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

          That's cool....does it work? :)

          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