0

I am a beginner, so please bear with me.

Let's say we have a listbox and a grid that contains textblocks on a window. But these are instanced from separate xaml files, and added to two different stackpanels in the window.

How can I bind the grid datasource to the listbox's selected item in runtime?

I guess what I need to do is to do this following xaml in runtime:

Grid DataContext="{Binding ElementName=lstContacts, Path=SelectedItem}"

Thanks.

Anne
  • 26,765
  • 9
  • 65
  • 71

1 Answers1

0

You could handle the Window's Loaded event, and in your handler: traverse your Window's children using VisualTreeHelper to find your ListBox based on its name, then set your Grid's DataContext.

Here is a question that discusses, and provides a method for, finding children in such a manner.

Community
  • 1
  • 1
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38