两个用户控件之间的通信 [英] Communication between Two UserControls
问题描述
大家好,
我开发了1个wpf应用程序,其中有2个UserControl,并将这两个用户控件放入这样的MainWindow中
在我的第一个UserControl中,我有一个带有命令EditingCommands.ToggleBullets
的切换按钮.
我想将此命令应用于放置在其他UserControl中的RichTextBox控件.
(以下说明)
Hi All,
I developed 1 wpf application in which i have 2 UserControls and i am placing these two user controls into a MainWindow like this
In my first UserControl i have an toggle button with command EditingCommands.ToggleBullets
i want to apply this command to RichTextBox control which is placed in a different UserControl
(explained below)
<usercontrol x:class="UserControlEx.BulletControl" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:src="clr-namespace:UserControlEx"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<dockpanel>
<togglebutton name="bulletsButton" command="EditingCommands.ToggleBullets">
CommandTarget="{Binding ElementName=myRtb}"
Content="Bullet"
DockPanel.Dock="Top"
ToolTip="Bullets">
</togglebutton>
</dockpanel>
</usercontrol>
在第二个UserControl中,我有一个RichTextBox
In my 2nd UserControl i have an RichTextBox
<usercontrol x:class="UserControlEx.RichTextControl" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<dockpanel>
<richtextbox x:name="myRtb" />
</dockpanel>
</usercontrol>
现在我将两个控件都放置到我的MainWindow
Now i am placing both control to my MainWindow
<window x:class="UserControlEx.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:UserControlEx"
Title="Window1" Height="300" Width="300">
<dockpanel>
<src:BulletControl DockPanel.Dock="Top" x:Name="userControl1" />
<src:RichTextControl x:Name="userControl2" />
</dockpanel>
</window>
现在,我想在按下MainWindow中的切换按钮时应用项目符号命令.
任何想法如何做到这一点.
在此先感谢
Ashish
Now i want to apply a bullet command when i press the toggle button in my MainWindow.
Any Idea How to do this.
Thanks in advance
Ashish
推荐答案
事件聚合可以胜任.
请参阅此处:
MSDN:事件聚合快速入门 [如何:使用Event Aggregator在两个用户控件之间进行通信 [ ^ ]
使用Mediator在用户控件之间进行通信: 2 [
Event Aggregation can do the job.
Refer here:
MSDN: Event Aggregation QuickStart[^]
How To : Communicate between two user-controls using Event Aggregator[^]
Using Mediator to communicate between user controls: Part 2[^]
这篇关于两个用户控件之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!