How to bind to a source inside a ListBox different from the ItemsSource already specified

前端 未结 1 1515
耶瑟儿~
耶瑟儿~ 2021-01-26 18:31

I have a ListBox inside a HubSection, whose Items are bound to a class \"players\" added to my DefaulViewModel via code behind. First I simply put a TextBox bound to the propert

相关标签:
1条回答
  • 2021-01-26 19:09

    You can walk up the visual tree and bind to an ancestors datacontext:

    {Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
    

    EX:

    {Binding Path=ListOfItems, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}
    

    that should give you the datacontext that the listbox has, so assuming your ListOfItems exists in that data context.

    Or you can name your control, and then bind to its datacontext by element name:

    {Binding ElementName=mySourceElement,Path=ListOfItems}
    
    0 讨论(0)
提交回复
热议问题