将事件从自定义控件对象传递到mainwidow中的对象 [英] Pass event from custom control object to object in mainwidow
本文介绍了将事件从自定义控件对象传递到mainwidow中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$
你好。
将自定义控制对象的事件传递给主窗口中的对象。
我创建一个简单的自定义对象什么是形状矩形, 在Mainwidnow我有另一个矩形。
当我在自定义控件上触摸矩形时,我希望主窗口上的矩形颜色发生变化。
怎么做?
一个明确的代码示例总是受欢迎的! :)
$
Hello.
Pass event from custom control object to object in mainwindow.
I create a simple custom object what is a shape rectangle, on Mainwidnow I have another rectangle.
When I touch down rectangle on custom control, I want the rectangle located on mainwindow change of color.
How can do that?
A clear code example is always welcome! :)
推荐答案
Very good and simple example show how doit :)
public partial class ServerConfigs : UserControl
{
// Create RoutedEvent
// This creates a static property on the UserControl, SettingsConfirmedEvent, which
// will be used by the Window, or any control up the Visual Tree, that wants to
// handle the event. This is the Custom Routed Event for more info on the
// RegisterRoutedEvent method
// https://msdn.microsoft.com/en-us/library/ms597876(v=vs.100).aspx
public static readonly RoutedEvent SettingConfirmedEvent =
EventManager.RegisterRoutedEvent("SettingConfirmedEvent", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(ServerConfigs));
// Create RoutedEventHandler
// This adds the Custom Routed Event to the WPF Event System and allows it to be
// accessed as a property from within xaml if you so desire
public event RoutedEventHandler SettingConfirmed
{
add { AddHandler(SettingConfirmedEvent, value); }
remove { RemoveHandler(SettingConfirmedEvent, value); }
}
....
// When the Save button on the User Control is clicked, use RaiseEvent to fire the
// Custom Routed Event
private void btnSave_Click(object sender, RoutedEventArgs e)
{
....
// Raise the custom routed event, this fires the event from the UserControl
RaiseEvent(new RoutedEventArgs(ServerConfigs.SettingConfirmedEvent));
....
}
}
<Window>
....
<uc1:ServerConfigs SettingConfirmed="Window_UserControl_SettingConfirmedEventHandlerMethod"
x:Name="ucServerConfigs" ></uc1:ServerConfigs>
...
</Window>
这篇关于将事件从自定义控件对象传递到mainwidow中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文