在WPF中将一个用户控件的控制权调用到另一个 [英] Call control of one user control to another in WPF

查看:85
本文介绍了在WPF中将一个用户控件的控制权调用到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我有两个用户控件。我想在我点击另一个用户控件的按钮时更改一个用户控件的背景颜色。我是WPF的新手。任何人都可以帮助我。



我尝试过:



我试图将用户控件调用到另一个用户控件。但它并没有帮助我改变用户控制的背景。



I have two user controls. I want to change the background color of one user control when i click on the button of another user controls. I am new in WPF. Can any one help me.

What I have tried:

I have tried to call a user control to another user control. But it doesn't help me to change the background of user control.

推荐答案

你的控件应该永远不会彼此了解。执行此操作的正确方法是在用户控件中公开需要更改其他内容的事件。主机窗口将订阅此事件并决定如何处理它。如果这意味着告诉其他用户控件改变它的背景,那就这样吧。



这也意味着你的其他用户控件必须暴露一些方法所以主机窗口可以调用该方法来改变背景颜色。
You controls should never know about each other. The proper way to do this would be to expose an event in the user control that wants something else changed. The host window would subscribe to this event and decide what to do with it. If that means telling the other user control to change it's background, so be it.

That also means your other user control would have to expose some method so the host window can call that method to change the background color.


这篇关于在WPF中将一个用户控件的控制权调用到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆