WPF用户控件中的按钮事件里,如何取得其所在的Window窗体?

kyoybs 2011-11-16 11:07:31
如T, 谢谢!
...全文
1697 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
西瓜巨人 2013-11-05
  • 打赏
  • 举报
回复
Window parentWindow=GetWindow(this);获取
Bullatus 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fallincloud 的回复:]

可以参照我博客里面的http://blog.csdn.net/fallincloud/article/details/6960255
FindParent的方法,用法Window w = FindParent<Window>(this);
[/Quote]
逐层查找父级,然后与指定类型对比,封装成了一个方法而已,不过用着会方便些
Tony萧 2011-11-21
  • 打赏
  • 举报
回复
可以用这个方法获取控件所在窗体
Button button = sender as Button;
Window targetWindow = Window.GetWindow(button);
fallincloud 2011-11-16
  • 打赏
  • 举报
回复
或者使用App.Current.MainWindow可以获得当前窗口
fallincloud 2011-11-16
  • 打赏
  • 举报
回复
可以参照我博客里面的http://blog.csdn.net/fallincloud/article/details/6960255
FindParent的方法,用法Window w = FindParent<Window>(this);
kyoybs 2011-11-16
  • 打赏
  • 举报
回复
用户控件里,this是用户控件本身, this.Parent是放置用户控件的容器, 比如窗体上的Grid或Panel。
我想得到窗体, 目前是通过逐级向上遍历的方法。 想知道有没有直接取得窗体对象的方法。
zxh198841 2011-11-16
  • 打赏
  • 举报
回复
是指的本窗体里面的控件吗?
sjfujiang 2011-11-16
  • 打赏
  • 举报
回复
可以通过this.Parent获得
幸福小6 2011-11-16
  • 打赏
  • 举报
回复
不懂你的意思,如果你的按钮处理事件就是在Window类里,直接用this就可以得到这个window啊。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧