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. ListBox Binding From Within A User Control

ListBox Binding From Within A User Control

Scheduled Pinned Locked Moved WPF
csswpfwcfhardwarehelp
6 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.
  • B Offline
    B Offline
    BlitzPackage
    wrote on last edited by
    #1

    Good people, I have a listbox embedded within a user control.   The user control is then housed within a data template.   I have created a dependency property for the user control; the type of this property is an observable collection that is intended to be the source of items for the listbox. The difficulty I am having is that the listbox will not display the items when the user control is part of the datatemplate.   It will display items when it is not part of the data template.   I am pasting code below.   Any help, advice or information would be really helpful.   Thanks in advance. Here is the code for the list box as it is in the user control. <pre> <ListBox SelectionChanged="ListBoxImages_SelectionChanged" IsSynchronizedWithCurrentItem="True" Margin="8,40.76,8,174.91" x:Name="ListBoxImages" Grid.Row="1" ItemsSource="{Binding Path=Photos, ElementName=UCAscensionImageManagement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource DataTemplateImageList}">           </ListBox> </pre> This is the user control, that contains the listbox.   It is in a datatemplate. <pre> <SG2_Ascension:UserControlAscensionImageManagement Margin="21.234,0,17.733,54" VerticalAlignment="Bottom" Height="475" x:Name="UCAIMEventPhotos" Width="615" Photos="{Binding Path=UCGeneralDataSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> Finally, this is the dependency property as it is coded in the user control <pre> public static DependencyProperty PhotosProperty = DependencyProperty.Register(                   "Photos", typeof(GeneralDataSeries), typeof(UserControlAscensionImageManagement)/*,                   new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRatingChanged))*/

    M 1 Reply Last reply
    0
    • B BlitzPackage

      Good people, I have a listbox embedded within a user control.   The user control is then housed within a data template.   I have created a dependency property for the user control; the type of this property is an observable collection that is intended to be the source of items for the listbox. The difficulty I am having is that the listbox will not display the items when the user control is part of the datatemplate.   It will display items when it is not part of the data template.   I am pasting code below.   Any help, advice or information would be really helpful.   Thanks in advance. Here is the code for the list box as it is in the user control. <pre> <ListBox SelectionChanged="ListBoxImages_SelectionChanged" IsSynchronizedWithCurrentItem="True" Margin="8,40.76,8,174.91" x:Name="ListBoxImages" Grid.Row="1" ItemsSource="{Binding Path=Photos, ElementName=UCAscensionImageManagement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource DataTemplateImageList}">           </ListBox> </pre> This is the user control, that contains the listbox.   It is in a datatemplate. <pre> <SG2_Ascension:UserControlAscensionImageManagement Margin="21.234,0,17.733,54" VerticalAlignment="Bottom" Height="475" x:Name="UCAIMEventPhotos" Width="615" Photos="{Binding Path=UCGeneralDataSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> Finally, this is the dependency property as it is coded in the user control <pre> public static DependencyProperty PhotosProperty = DependencyProperty.Register(                   "Photos", typeof(GeneralDataSeries), typeof(UserControlAscensionImageManagement)/*,                   new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRatingChanged))*/

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

      On the ListBox you have

      ItemsSource="{Binding Path=Photos, ElementName=UCAscensionImageManagement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

      In the usercontrol you have

      x:Name="UCAIMEventPhotos" Photos="{Binding Path=UCGeneralDataSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

      I assume you mean to bind the ListBox ItemsSource to the UserControl Photos property, correct? If so, you used the wrong ElementName in the ListBox ItemsSource binding. You could also try something like this:

      ItemsSource="{Binding Path=Photos, RelativeSource={RelativeSource AncestorType={x:Type SG2_Ascension:UserControlAscensionImageManagement}}, Mode=OneWay}"

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

      B 1 Reply Last reply
      0
      • M Mark Salsbery

        On the ListBox you have

        ItemsSource="{Binding Path=Photos, ElementName=UCAscensionImageManagement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

        In the usercontrol you have

        x:Name="UCAIMEventPhotos" Photos="{Binding Path=UCGeneralDataSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

        I assume you mean to bind the ListBox ItemsSource to the UserControl Photos property, correct? If so, you used the wrong ElementName in the ListBox ItemsSource binding. You could also try something like this:

        ItemsSource="{Binding Path=Photos, RelativeSource={RelativeSource AncestorType={x:Type SG2_Ascension:UserControlAscensionImageManagement}}, Mode=OneWay}"

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

        B Offline
        B Offline
        BlitzPackage
        wrote on last edited by
        #3

        Thanks Mark, It seems you are on to the solution.   The only error it's giving me is that it says "the type reference cannot find a public type named 'UserControlAscensionImageManagement'. This is unexpected since the listbox, and thus this declaration, is occurring within the UserControlAscensionImageManagement XAML file.   Wow! At any rate, let me know if you have any additional thoughts.   I am continuing to work on finding a solution on my end. Thanks for any additional information you can provide.

        M 1 Reply Last reply
        0
        • B BlitzPackage

          Thanks Mark, It seems you are on to the solution.   The only error it's giving me is that it says "the type reference cannot find a public type named 'UserControlAscensionImageManagement'. This is unexpected since the listbox, and thus this declaration, is occurring within the UserControlAscensionImageManagement XAML file.   Wow! At any rate, let me know if you have any additional thoughts.   I am continuing to work on finding a solution on my end. Thanks for any additional information you can provide.

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

          BlitzPackage wrote:

          The only error it's giving me is that it says "the type reference cannot find a public type named 'UserControlAscensionImageManagement'.

          Even after a rebuild? Is the class marked public?

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

          B 1 Reply Last reply
          0
          • M Mark Salsbery

            BlitzPackage wrote:

            The only error it's giving me is that it says "the type reference cannot find a public type named 'UserControlAscensionImageManagement'.

            Even after a rebuild? Is the class marked public?

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

            B Offline
            B Offline
            BlitzPackage
            wrote on last edited by
            #5

            Hmmmm...haven't tried a rebuild.   Also, yes the class is marked public.   I will try a rebuild shortly. Rebuilds usually erase all of my test data in the database and force me to start from scratch.   However, I will try it and let you know. Thanks again.

            M 1 Reply Last reply
            0
            • B BlitzPackage

              Hmmmm...haven't tried a rebuild.   Also, yes the class is marked public.   I will try a rebuild shortly. Rebuilds usually erase all of my test data in the database and force me to start from scratch.   However, I will try it and let you know. Thanks again.

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

              BlitzPackage wrote:

              Rebuilds usually erase all of my test data in the database and force me to start from scratch.

              hmm ok. That doesn't sound like a friendly environment for WPF development...

              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