wpf 如何将 RoutedEventHandler 正确传递给 UserControl?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19545694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a RoutedEventHandler correctly to a UserControl?
提问by Gerard
I have a UserControlc:MenuButtonswith an ItemsControland the ItemTemplateis a DataTemplatewith a RadioButton.
我有一个UserControlc:MenuButtons和一个ItemsControl,ItemTemplate是一个DataTemplate和一个RadioButton。
In the main wpf window I use the UserControllike this:
在 wpf 主窗口中,我使用UserControl如下:
<c:MenuButtons x:Name="MenuProjects"
SelectionChanged="{Binding d:MenuClick}"
Height="35"
MenuItems="{Binding Source={x:Static d:Main.Projects}}" />
I would like to have the Checkedevent of the RadioButtonto bubble up via the UserControlto the MenuClickhandler in the Main window code-behind and handle it there. This is a view-only thing so I do not use ICommandor model-patterns here.
我想让 toChecked事件RadioButton通过主窗口代码隐藏中UserControl的MenuClick处理程序冒泡并在那里处理。这是一个仅限视图的东西,所以我ICommand在这里不使用或模型模式。
The RadioButtonCheckedevent is a RoutedEventHandler, so is SelectionChangedin c:MenuButtonsand also MenuClickin my main window code-behind.
该RadioButtonChecked事件是一个RoutedEventHandler,所以是SelectionChanged在c:MenuButtons并MenuClick在我的主窗口中的代码隐藏。
I cannot get it to work.
我无法让它工作。
In the class MenuButtons I have the following code:
在 MenuButtons 类中,我有以下代码:
RoutedEventHandler handler;
public event RoutedEventHandler SelectionChanged
{
add { handler += value; }
remove { handler -= value; }
}
but the add is not entered despite the data-binding. Why not?
但是尽管有数据绑定,但没有输入添加。为什么不?
And: suppose it would be bound, how should I declare the binding in the DataTemplate?
I tried Checked="{Binding ElementName=root, Path=SelectionChanged}"which gives a XamlParseException on this particular attempt of binding (Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.)
并且:假设它会被绑定,我应该如何在 DataTemplate 中声明绑定?
我尝试Checked="{Binding ElementName=root, Path=SelectionChanged}"在这个特定的绑定尝试中给出 XamlParseException(无法将类型为“System.Reflection.RuntimeEventInfo”的对象转换为类型“System.Reflection.MethodInfo”。)
It turns out to be straightforward:
In the class MenuButtons you get:
结果很简单:
在 MenuButtons 类中,您将获得:
public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MenuButtons));
public event RoutedEventHandler SelectionChanged
{
add { AddHandler(SelectionChangedEvent, value); }
remove { RemoveHandler(SelectionChangedEvent, value); }
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
RoutedEventArgs eventargs = new RoutedEventArgs(MenuButtons.SelectionChangedEvent);
RaiseEvent(eventargs);
}
In the xaml of the UserControl you add:
在 UserControl 的 xaml 中添加:
<RadioButton Checked="RadioButton_Checked"
And in the main form you have:
在主要形式中,您有:
<c:MenuButtons SelectionChanged="MenuProjects_SelectionChanged"
There luckily e.OriginalSourcegives the information for handling the event.
幸运的是e.OriginalSource提供了处理事件的信息。
采纳答案by dev hedgehog
You should check those links out:
您应该检查这些链接:
http://msdn.microsoft.com/en-us/library/ms752288.aspx
http://msdn.microsoft.com/en-us/library/ms752288.aspx
And also this:
还有这个:
http://msdn.microsoft.com/en-us/library/ms742806.aspx
http://msdn.microsoft.com/en-us/library/ms742806.aspx
The tutorials will show you how to create routed events and how to subscribe them properly to elements.
这些教程将向您展示如何创建路由事件以及如何将它们正确订阅到元素。
Btw, add { handler += value; }is wrong.
顺便说一句,add { handler += value; }错了。
This is how you should write it : add { this.AddHandler(MyRoutedEvent...);}
你应该这样写:add { this.AddHandler(MyRoutedEvent...);}

