如何绑定到所属窗口的控件属性(来自对话框窗口)?
我的朋友们,
我在 WPF 中遇到了一个我无法解决的问题。我有两个窗口,Mainwindow 和 Window1。我想将子窗口中的 ListView 绑定到父窗口中的控件属性。详细信息:
Mainwindow 有一个在 XAML 中声明的控件,
再往下我有一个 Listview,它由“View”的公共属性提供,“Session.Events”(可观察集合)
<ListView ItemsSource="{Binding ElementName=View, Path=Session.Events}"/>
工作正常,但现在我有第二个窗口以这种方式从主窗口生成:
Window1 MyWin1 = new Window1();
MyWin1.Owner = this;
MyWin1.ShowDialog();
并且第二个窗口有一个 ListView,它也需要由我的“视图”控件提供。我想通过捆绑来做到这一点,但我咬掉了牙齿。无论我尝试什么,它都不起作用。我确实有一个通过代码隐藏的工作版本...
Window1 Parent = (Window1)this.Owner;
MyListView.ItemsSource = Parent.CCView.Session.Events;
但我更喜欢在 XAML 中进行绑定并保存额外的代码。我也希望它能帮助我更好地理解绑定,这对我来说在某种程度上仍然是一个谜。
非常感谢您并致以最诚挚的问候,
保罗
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();
MyWin1.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,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以跨 XAML 的逻辑树进行绑定。第二个窗口不是第一个窗口树的一部分。我认为在您的情况下最简单的方法应该是将 DataContext 传递到您的子窗口:
You 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: