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