How to bind to an owning window's control-property (from a dialog window)?
My friends,
i Have a problem in WPF which i just cannot solve. I have two Windows, Mainwindow and Window1. I'd like to bind a ListView in my child-window to a controls property in the parent-window. In detail:
Mainwindow has a control declared in XAML,
<local:MyControl x:Name="View"/>
Further down i have a Listview which get's fed by a public property of 'View', 'Session.Events' (Observable Collection)
<ListView ItemsSource="{Binding ElementName=View, Path=Session.Events}"/>
Which works fine, but now i have a second Window spawned from Mainwindow in such manner:
Window1 MyWin1 = new Window1();
MyW开发者_StackOverflow中文版in1.Owner = this;
MyWin1.ShowDialog();
And this second window has a ListView which also needs to be fed by my 'View' control. I'd like to do it via binding but i bite my teeth out. It does not work, whatever i try. I do have a working version via code-behind ...
Window1 Parent = (Window1)this.Owner;
MyListView.ItemsSource = Parent.CCView.Session.Events;
But i would prefer doing the bind in XAML and save the extra-code. Also i hope it will help me to understand bindings better, which for me are still a mystery to some extend.
Thank you so much and my best regards,
PaulYou can bind across the logical tree of your XAML. The second window is not part of the first window's tree. I'd think the most simple way in your situation should be to pass over the DataContext to your child window:
MyWin1 = new Window1 {
DataContext = this.DataContext,
Owner = this
};
MyWin1.ShowDialog();
精彩评论