locked
how do I bind a control to a nested collection RRS feed

  • Question

  • MyUserControl defines an ObservableCollection

    MyUserControl is instantiated in MainWindow.xaml

    unable to bind listbox in MainWindow.xaml to the ObservableCollection in MyUserControl

    this approach produces an error:

    <Window.Resources>

    <local:myUserControl.MyCollection x:key="whatever"/>

    </Window.Resources>

    .....

    Also tried using ObjectDataProvider and binding to a method in the class:

     <ObjectDataProvider ObjectType="{x:Type local:myUserControl}" MethodName="GetMyCollection" x:Key="xxx"/>

    and then in the body of MainWindow.xaml

      <ListBox ItemsSource="{Binding Source={StaticResource xxx}}"></ListBox>

    nor does this work

       <ListBox ItemsSource="{Binding Source={StaticResource xxx}, BindsDirectlyToSource=True}"></ListBox>

    Any help greatly appreciated

     

    Friday, May 20, 2011 12:09 AM

Answers

All replies

  • unable to bind listbox in MainWindow.xaml to the ObservableCollection in MyUserControl

    What error if any do you get ? Is it compile time or empty listbox ?

    Do you have a data template ?

     

    AT

    Friday, May 20, 2011 6:36 AM
  • this code produces an error

    <Window.Resources>

    <local:myUserControl.MyCollection x:key="whatever"/>

    </Window.Resources>

    specifically, local:myUserControl.MyCollection is interpreted by WPF as being an attached property and is not the correct syntax to specify a collection contained within an object as a binding source (myUserControl in this case is an object within MainWindow). 

    ObjectDataProvider bind to method does not work because there is no mechanism I can find to cause the method to be called as items are to the collection within the UserControl


    Sunday, May 22, 2011 7:00 PM
  • To reference MyCollection like that, MyCollection would have to be a Dependency Property. If you want to get at that property, you're probably going to have to give the MyUserControl a name (or use a RelativeSource binding), then {Binding ElementName=myUserControl,Path=DataContext.MyCollection}.


    Warm regards,

    Matt

    • Proposed as answer by Yves.Z Thursday, May 26, 2011 9:00 AM
    • Marked as answer by JB90125 Friday, May 27, 2011 12:26 PM
    Monday, May 23, 2011 2:58 AM
  • Hi JB90125,

    Does Matt's reply help you figured it out?

    If you still have any concerns please feel free to let us know.

    Regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 26, 2011 9:01 AM
    • Marked as answer by JB90125 Friday, May 27, 2011 12:25 PM
    Friday, May 27, 2011 12:33 AM
  • Thank you Matt!

    Since this issue has beed resolved, we are not merging it. However, I shall keep the link referred to dupliacate thread in case someone searchs it.

    Thanks again for your help!

    Have a nice day!


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, May 29, 2011 4:12 AM