{"id":5328,"date":"2024-09-19T19:01:01","date_gmt":"2024-09-19T11:01:01","guid":{"rendered":""},"modified":"2024-09-19T19:01:01","modified_gmt":"2024-09-19T11:01:01","slug":"\u8def\u7531 wps \u6309\u94ae \u4f5c\u7528_\u8def\u7531\u5668\u51b2\u7a81\u600e\u4e48\u89e3\u51b3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5328.html","title":{"rendered":"\u8def\u7531 wps \u6309\u94ae \u4f5c\u7528_\u8def\u7531\u5668\u51b2\u7a81\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"

\u5199\u5728\u524d\u9762<\/strong><\/span><\/p>\n

\u672c\u6587\u4e00\u5f00\u59cb\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u4f7f\u7528WPF\u8def\u7531\u4e8b\u4ef6\u7684\u5b9e\u4f8b\uff0c\u56e0\u4e3a\u672c\u6587\u6240\u6709\u7684\u8868\u8ff0\u90fd\u5c06\u57fa\u4e8e\u8be5\u5b9e\u4f8b\u3002\u800c\u672c\u6587\u6240\u7ed9\u5b9e\u4f8b\u6765\u81ea\u4e8e\u300aWPF\u81ea\u5b9a\u4e49\u8def\u7531\u4e8b\u4ef6\u300b\u4e00\u6587\uff0c\u5728\u300aWPF\u81ea\u5b9a\u4e49\u8def\u7531\u4e8b\u4ef6\u300b\u4e00\u6587\u4e2d\u4f1a\u5bf9\u5b9e\u4f8b\u4ee3\u7801\u505a\u8be6\u7ec6\u8bf4\u660e\uff0c\u6240\u4ee5\uff0c\u5927\u5bb6\u5728\u9605\u8bfb\u672c\u6587\u5b9e\u4f8b\u4ee3\u7801\u671f\u95f4\u82e5\u5b58\u5728\u7591\u95ee\uff0c\u53ef\u4ee5\u5148\u53bb\u770b\u770b\u300aWPF\u81ea\u5b9a\u4e49\u8def\u7531\u4e8b\u4ef6\u300b\u4e00\u6587\uff0c\u770b\u662f\u5426\u80fd\u4ece\u4e2d\u83b7\u5f97\u4f60\u60f3\u8981\u7684\u89e3\u7b54\u3002<\/span><\/p>\n


<\/span><\/p>\n

\u672c\u6587\u5b9e\u4f8b<\/strong><\/span><\/p>\n

1 \u65b0\u5efaDetailReportEventArgs\u7c7b\uff0c\u8be5\u7c7b\u6d3e\u751f\u81eaRoutedEventArgs\u7c7b\uff0cRoutedEventArgs\u7c7b\u5305\u542b\u4e0e\u8def\u7531\u4e8b\u4ef6\u76f8\u5173\u7684\u72b6\u6001\u4fe1\u606f\u548c\u4e8b\u4ef6\u6570\u636e\u3002DetailReportEventArgs\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u5c5e\u6027EventTime\u4e0eEventPublishr\uff0cEventTime\u5c5e\u6027\u8bb0\u5f55\u65f6\u95f4\u7684\u53d1\u751f\u65f6\u95f4\uff0cEventPublishr\u5c5e\u6027\u8bb0\u5f55\u4e8b\u4ef6\u7684\u53d1\u5e03\u8005\u3002\u4e0b\u9762\u662fDetailReportEventArgs\u7c7b\u7684\u5b8c\u6574\u4ee3\u7801\u3002<\/p>\n<\/p>\n

\/\/************************************************************ \/\/ \/\/ WPF\u8def\u7531\u4e8b\u4ef6\u793a\u4f8b\u4ee3\u7801 \/\/ \/\/ Author\uff1a\u4e09\u4e94\u6708\u513f \/\/ \/\/ Date\uff1a2014\/08\/31 \/\/ \/\/ http:\/\/blog.csdn.net\/yl2isoft \/\/ \/\/************************************************************ using System; using System.Windows; namespace WpfRoutedEventExp { public class DetailReportEventArgs : RoutedEventArgs { public DetailReportEventArgs(RoutedEvent routedEvent, object source) :base(routedEvent,source){} public DateTime EventTime { get; set; } public string EventPublisher { get; set; } } }<\/code><\/pre>\n<\/p>\n

2 \u65b0\u5efaDetailReportButton\u7c7b\uff0c\u8be5\u7c7b\u6d3e\u751f\u81eaButton\u7c7b\uff0c\u4e3a\u8be5\u7c7b\u6dfb\u52a0\u8def\u7531\u4e8b\u4ef6DetailReportEvent\u3002\u4e0b\u9762\u662fDetailReportButton\u7c7b\u7684\u5b8c\u6574\u4ee3\u7801\u3002<\/p>\n<\/p>\n

\/\/************************************************************ \/\/ \/\/ WPF\u8def\u7531\u4e8b\u4ef6\u793a\u4f8b\u4ee3\u7801 \/\/ \/\/ Author\uff1a\u4e09\u4e94\u6708\u513f \/\/ \/\/ Date\uff1a2014\/08\/31 \/\/ \/\/ http:\/\/blog.csdn.net\/yl2isoft \/\/ \/\/************************************************************ using System; using System.Windows; using System.Windows.Controls; namespace WpfRoutedEventExp { public class DetailReportButton : Button { public static readonly RoutedEvent DetailReportEvent = EventManager.RegisterRoutedEvent(\"DetailReport\",RoutingStrategy.Bubble,typeof(EventHandler<DetailReportEventArgs>),typeof(DetailReportButton)); public event RoutedEventHandler DetailReport { add { this.AddHandler(DetailReportEvent, value); } remove { this.RemoveHandler(DetailReportEvent, value); } } protected override void OnClick() { base.OnClick(); DetailReportEventArgs args = new DetailReportEventArgs(DetailReportEvent, this); args.EventPublisher = this.ToString(); args.EventTime = DateTime.Now; this.RaiseEvent(args); } } }<\/code><\/pre>\n<\/p>\n

3 \u4e0b\u9762\u7ed9\u51fa\u4f7f\u7528DetailReportButton \u7c7b\u7684\u5b8c\u6574\u4ee3\u7801\uff0c\u5305\u62ec\u753b\u9762\u4ee3\u7801\u4ee5\u53ca\u753b\u9762\u540e\u7aef\u4ee3\u7801\u4e24\u90e8\u5206\u3002<\/p>\n

\u7a0b\u5e8f\u753b\u9762\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

<Window x:Class=\"WpfRoutedEventExp.MainWindow\" xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\" xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\" xmlns:local=\"clr-namespace:WpfRoutedEventExp\" Title=\"MainWindow\" Height=\"350\" Width=\"525\"> <Grid x:Name=\"Grid_FirstLayer\" > <Grid x:Name=\"Grid_SecondLayer\" > <Grid x:Name=\"Grid_ThirdLayer\" > <local:DetailReportButton x:Name=\"Button_Confirm\" Width=\"100\" Height=\"100\" Content=\"Click Me\" Margin=\"142,111,250,100\" \/> <\/Grid> <\/Grid> <\/Grid> <\/Window><\/code><\/pre>\n<\/p>\n

\u7a0b\u5e8f\u753b\u9762\u7684\u540e\u7aef\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

\/\/************************************************************ \/\/ \/\/ WPF\u8def\u7531\u4e8b\u4ef6\u793a\u4f8b\u4ee3\u7801 \/\/ \/\/ Author\uff1a\u4e09\u4e94\u6708\u513f \/\/ \/\/ Date\uff1a2014\/08\/31 \/\/ \/\/ http:\/\/blog.csdn.net\/yl2isoft \/\/ \/\/************************************************************ using System.Windows; namespace WpfRoutedEventExp { \/\/\/ <summary> \/\/\/ MainWindow.xaml \u7684\u4ea4\u4e92\u903b\u8f91 \/\/\/ <\/summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Grid_ThirdLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Grid_SecondLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Grid_FirstLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Button_Confirm.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); } private void Button_Clicked1(object sender, RoutedEventArgs e) { FrameworkElement ele = sender as FrameworkElement; DetailReportEventArgs dre = e as DetailReportEventArgs; MessageBox.Show(dre.EventPublisher + \"-->\" + ele.Name + \";\" + dre.EventTime); } } }<\/code><\/pre>\n

4 \u6267\u884c\u7a0b\u5e8f\uff0c\u5f97\u5230\u4e0b\u56fe\u6240\u793a\u6548\u679c\u3002<\/span> <\/p>\n

\"\u8def\u7531 <\/p>\n

\u56fe1 \u7a0b\u5e8f\u8fd0\u884c\u6548\u679c\u56fe<\/strong><\/p>\n

\u7ed3\u679c\u663e\u793a\uff1a\u5355\u51fb\u753b\u9762\u6309\u94ae\uff0c\u89e6\u53d1\u8def\u7531\u4e8b\u4ef6\uff0c\u8def\u7531\u4e8b\u4ef6\u7684\u6d88\u606f\u4fbf\u4ece\u4e8b\u4ef6\u7684\u89e6\u53d1\u8005\u5f00\u59cb\u5411\u5b83\u7684\u4e0a\u7ea7\u5bb9\u5668\u63a7\u4ef6\u4e00\u5c42\u4e00\u5c42\u7684\u5f80\u5916\u4f20\uff0c\u76f4\u5230\u6700\u5916\u5c42\u7684\u5bb9\u5668\u63a7\u4ef6\u4e3a\u6b62\u3002<\/p>\n

 <\/p>\n

\u4f60\u6211\u7684\u7ea6\u5b9a<\/strong><\/p>\n

\u56e0\u4e3a\u4e0b\u9762\u7684\u8bf4\u660e\u4e2d\u4f1a\u53cd\u590d\u4fee\u6539\u5b9e\u4f8b\u4ee3\u7801\uff0c\u73b0\u5728\u7ed9\u51fa\u7ea6\u5b9a\uff1a<\/p>\n

\u6bcf\u4e00\u6b21\u4fee\u6539\u5728\u8bf4\u660e\u5b8c\u76f8\u5173\u7684\u77e5\u8bc6\u70b9\u540e\u90fd\u5c06\u56de\u9000\u5230\u4fee\u6539\u524d\u7684\u72b6\u6001\u3002<\/strong><\/span><\/span><\/p>\n

 <\/p>\n

\u6211\u7684\u65b0\u95fb\u53d1\u5e03\u4f1a<\/strong><\/p>\n

\u4e0b\u9762\u5c06\u4ee5\u95ee\u7b54\u7684\u65b9\u5f0f\u6765\u603b\u7ed3\u6211\u5728\u4f7f\u7528WPF\u8def\u7531\u4e8b\u4ef6\u65f6\u9047\u5230\u6216\u60f3\u5230\u7684\u51e0\u4e2a\u5c0f\u95ee\u9898\u3002<\/p>\n

<\/p>\n

Q1 \uff1a\u9664\u4e86\u4f7f\u7528AddHandler\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5c06\u60f3\u76d1\u542c\u7684\u4e8b\u4ef6\u4e0e\u4e8b\u4ef6\u7684\u5904\u7406\u5668\u5173\u8054\u8d77\u6765\uff1f<\/span><\/p>\n

\u53ef\u4ee5\u5728XAML\u4ee3\u7801\u4e2d\u5c06\u76d1\u542c\u7684\u4e8b\u4ef6\u4e0e\u4e8b\u4ef6\u7684\u5904\u7406\u5668\u5173\u8054\u8d77\u6765\u3002\u6309\u7167\u4e0b\u9762\u6240\u7ed9\u65b9\u6cd5\u4fee\u6539\u5b9e\u4f8b\u4ee3\u7801\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u8def\u7531\u4e8b\u4ef6\u3002<\/p>\n

\uff081\uff09\u6ce8\u91ca\u6389\u4ee5\u4e0b\u56db\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n

this.Grid_ThirdLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Grid_SecondLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Grid_FirstLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1)); this.Button_Confirm.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1));<\/code><\/pre>\n<\/p>\n

\uff082\uff09\u5728XAML\u4ee3\u7801\u4e2d\u5c06\u76d1\u542c\u7684\u4e8b\u4ef6\u4e0e\u4e8b\u4ef6\u7684\u5904\u7406\u5668\u5173\u8054\u8d77\u6765\uff0c\u5177\u4f53\u64cd\u4f5c\u89c1\u4e0b\u56fe\u8bf4\u660e\u3002<\/p>\n

\"\u8def\u7531  <\/span><\/p>\n

\u56fe2 \u5728XAML\u4e2d\u5c06\u76d1\u542c\u7684\u4e8b\u4ef6\u4e0e\u4e8b\u4ef6\u5904\u7406\u5668\u5173\u8054<\/strong><\/p>\n

\uff083\uff09\u56fe2\u663e\u793a\uff0c\u6b64\u65f6\u4f7f\u7528\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u4e0d\u518d\u662fButton_Clicked1\uff0c\u800c\u662fButton_Clicked2\uff0c\u4e0b\u9762\u7ed9\u51faButton_Clicked2\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n

private void Button_Clicked2(object sender, DetailReportEventArgs e) { FrameworkElement ele = sender as FrameworkElement; MessageBox.Show(e.EventPublisher + \"-->\" + ele.Name + \";\" + e.EventTime); }<\/code><\/pre>\n<\/p>\n

\u5c06Button_Clicked2\u4e0eButton_Clicked1\u8fdb\u884c\u5bf9\u6bd4\uff0c\u53d1\u73b0\u4e24\u8005\u7684\u672c\u8d28\u533a\u522b\u5c31\u662f\uff1a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u4e0d\u76f8\u540c\u3002<\/p>\n

Button_Clicked2\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u4e3aDetailReportEventArgs\uff0c\u800cButton_Clicked1\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u4e3aRoutedEventArgs\u3002<\/p>\n

\u7ee7\u7eedQ\uff1a\u9020\u6210\u8fd9\u79cd\u533a\u522b\u7684\u539f\u56e0\u53c8\u662f\u4ec0\u4e48\u5462\uff1f<\/span><\/p>\n

\u90a3\u662f\u56e0\u4e3a\u5728\u4f7f\u7528AddHandler\u65b9\u6cd5\u65f6\uff0c\u4f20\u5165\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u5fc5\u987b\u6ee1\u8db3RoutedEventHandler\u7684\u5b9a\u4e49\uff0c\u800cRoutedEventHandler\u7684\u5b9a\u4e49\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n

public delegate void RoutedEventHandler(object sender, RoutedEventArgs e)<\/code><\/pre>\n<\/p>\n

\u5f88\u663e\u7136\uff0cRoutedEventHandler\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u59d4\u6258\uff0c\u800c\u4e0e\u8be5\u59d4\u6258\u80fd\u591f\u5339\u914d\u7684\u65b9\u6cd5\u5fc5\u987b\u6ee1\u8db3\uff1a<\/p>\n

\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3aobject\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3aRoutedEventArgs\u7c7b\u578b\uff0c\u4e14\u4e0d\u5177\u6709\u8fd4\u56de\u503c\u3002<\/p>\n

\u6240\u4ee5\u5b9e\u4f8b\u4e2d\u4f7f\u7528\u4e86\u6ee1\u8db3\u8be5\u59d4\u6258\u8981\u6c42\u7684Button_Clicked1\u3002<\/p>\n

\u63a5\u7740\u9610\u8ff0\uff0cButton_Clicked2\u5b58\u5728\u7684\u539f\u56e0\u3002<\/p>\n

\u67e5\u770b\u81ea\u5b9a\u4e49\u8def\u7531\u4e8b\u4ef6\u7684\u5b9a\u4e49\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

public static readonly RoutedEvent DetailReportEvent = EventManager.RegisterRoutedEvent(\"DetailReport\",RoutingStrategy.Bubble,typeof(EventHandler<DetailReportEventArgs>),typeof(DetailReportButton));<\/code><\/pre>\n<\/p>\n

\u4ece\u5b9a\u4e49\u4e2d\u53ef\u4ee5\u627e\u5230\u8def\u7531\u4e8b\u4ef6\u8981\u6c42\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u7684\u7c7b\u578b\uff0c\u4e3aEventHandler<DetailReportEventArgs>\u3002<\/p>\n

\u540c\u6837\u53ef\u4ee5\u67e5\u770bEventHandler<DetailReportEventArgs>\u7684\u5b9a\u4e49\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n

public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);<\/code><\/pre>\n<\/p>\n

\u5c06\u5176\u4e2d\u7684TEventArgs\u66ff\u6362\u6210DetailReportEventArgs\uff0c\u5f97\u5230\u4ee5\u4e0b\u5b9a\u4e49\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

public delegate void EventHandler<DetailReportEventArgs>(object sender, DetailReportEventArgs e);<\/code><\/pre>\n<\/p>\n

\u770b\u5230\u8fd9\u4e2a\u5b9a\u4e49\uff0c\u6211\u60f3\u5927\u5bb6\u4e5f\u5c31\u77e5\u9053\u4e86Button_Clicked2\u5b58\u5728\u7684\u539f\u56e0\u4e86\u5427\u3002<\/p>\n

 <\/p>\n

Q2: \u65e2\u7136\u4e3a\u8def\u7531\u4e8b\u4ef6\u589e\u52a0\u4e86CLR\u4e8b\u4ef6\u5305\u88c5\uff0c\u90a3\u4e48\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u201c+=\u201d\u4e0e\u201c-=\u201d\u7b26\u53f7\u6765\u64cd\u4f5c\u8def\u7531\u4e8b\u4ef6?<\/span><\/p>\n

\u5f53\u7136\u53ef\u4ee5\u3002<\/p>\n

\u4e0b\u9762\u5c06\u901a\u8fc7\u4fee\u6539\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u6f14\u793a\u6765\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002<\/p>\n

\u6ce8\u91ca\u6389\u5b9e\u4f8b\u4e2d\u4e00\u5207\u5c06\u4e8b\u4ef6\u4e0e\u4e8b\u4ef6\u5904\u7406\u5668\u5173\u8054\u8d77\u6765\u7684\u4ee3\u7801\uff08\u753b\u9762\u4ee3\u7801\u4ee5\u53ca\u753b\u9762\u540e\u7aef\u4ee3\u7801\u4e2d\u5747\u8981\u8fd9\u4e48\u505a\uff09\u3002<\/p>\n

\u5728\u753b\u9762\u540e\u7aef\u4ee3\u7801\u4e2d\u589e\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

this.Button_Confirm.DetailReport += Button_Clicked1;<\/code><\/pre>\n<\/p>\n

\u8fd0\u884c\u7a0b\u5e8f\uff0c\u70b9\u51fb\u6309\u94ae\uff0c\u5f97\u5230\u4ee5\u4e0b\u6d88\u606f\u6846\uff1a<\/p>\n

\"\u8def\u7531 <\/p>\n

\u56fe3 \u4f7f\u7528\u201c+=\u201d\u8ba2\u9605\u4e8b\u4ef6\u540e\u7684\u8fd0\u884c\u6548\u679c\u56fe<\/strong><\/p>\n

\u4e8b\u4ef6\u5904\u7406\u5668\u88ab\u6b63\u786e\u6267\u884c\uff0c\u6b64\u65f6\u7684\u8def\u7531\u4e8b\u4ef6\u4e0e\u666e\u901a\u7684CLR\u4e8b\u4ef6\u76f8\u540c\uff0c\u76f4\u63a5\u5c06\u6d88\u606f\u4f20\u9012\u7ed9\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u3002<\/p>\n

\u63a5\u7740\uff0c\u518d\u589e\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

this.Button_Confirm.DetailReport -= Button_Clicked1;<\/code><\/pre>\n<\/p>\n

\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u70b9\u51fb\u6309\u94ae\uff0c\u8fd9\u4e0b\u5b50\uff0c\u5c31\u201c\u5475\u5475\u201d\u4e86\uff0c\u6211\u60f3\u4f60\u61c2\u7684\u3002<\/p>\n

 <\/p>\n

Q3 :\u540c\u4e00\u5bb9\u5668\u4e2d\u7684\u63a7\u4ef6\u4e4b\u95f4\u80fd\u591f\u76f8\u4e92\u76d1\u542c\u8def\u7531\u4e8b\u4ef6\u7684\u6d88\u606f\u5417\uff1f<\/span><\/p>\n

\u6309\u7167\u4e0b\u9762\u7684\u63cf\u8ff0\u4fee\u6539\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n

\uff081\uff09\u5728DetailReportButton\u6309\u94ae\u524d\u589e\u52a0\u540d\u4e3aButton_SameLayer\u7684\u6309\u94ae\u3002<\/p>\n<\/p>\n

<Grid x:Name=\"Grid_ThirdLayer\" > <Button x:Name=\"Button_SameLayer\"\/> <local:DetailReportButton x:Name=\"Button_Confirm\" Width=\"100\" Height=\"100\" Content=\"Click Me\" Margin=\"142,111,250,100\" \/> <\/Grid><\/code><\/pre>\n<\/p>\n

\uff082\uff09\u4f7fButton_SameLayer\u76d1\u542cDetailReportButton\u7684\u4e8b\u4ef6DetailReportEvent\u3002           <\/p>\n<\/p>\n

 this.Button_SameLayer.AddHandler(DetailReportButton.DetailReportEvent, new RoutedEventHandler(Button_Clicked1));<\/code><\/pre>\n<\/p>\n

\uff083\uff09\u8fd0\u884c\u7a0b\u5e8f\uff0c\u53d1\u73b0Button_SameLayer\u5e76\u6ca1\u6709\u76d1\u542c\u5230\u8def\u7531\u4e8b\u4ef6\u7684\u5230\u6765\u3002<\/p>\n

\u7531\u6b64\u53ef\u89c1\uff0c\u540c\u4e00\u5bb9\u5668\u4e2d\u7684\u63a7\u4ef6\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u76d1\u542c\u8def\u7531\u4e8b\u4ef6\u7684\u6d88\u606f\u3002<\/p>\n

 <\/p>\n

Q4:\u5982\u4f55\u8ba9\u4e00\u4e2a\u8def\u7531\u4e8b\u4ef6\u4f20\u9012\u81f3\u67d0\u4e00\u4e2a\u8282\u70b9\u5904\u4e0d\u518d\u7ee7\u7eed\u4f20\u9012\uff1f<\/span><\/p>\n

\u5728\u4e8b\u4ef6\u5904\u7406\u65b9\u6cd5Button_Clicked1\u4e2d\uff0c\u589e\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n

if (ele == this.Grid_SecondLayer) { e.Handled = true; }<\/code><\/pre>\n

\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6d88\u606f\u4f20\u9012\u81f3Grid_SecondLayer\u63a7\u4ef6\u540e\u5c06\u4e0d\u4f1a\u7ee7\u7eed\u4f20\u9012\u4e0b\u53bb\u3002<\/span> <\/p>\n

\u56e0\u4e3a\uff0c\u201ce.Handled= true\u201d\u7684\u610f\u601d\u662f\u8bf4\uff1a\u8be5\u8def\u7531\u4e8b\u4ef6\u5df2\u7ecf\u88ab\u5904\u7406\u4e86\uff0c\u4e0d\u9700\u8981\u518d\u7ee7\u7eed\u4f20\u9012\u4e0b\u53bb\u3002<\/p>\n

 <\/p>\n

Q<\/span>5: RoutedEventArgs\u7684Source\u4e0eOriginalSource\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/span><\/span><\/p>\n

\u4e3a\u4e86\u9610\u8ff0\u8fd9\u4e2a\u4e3b\u9898\uff0c\u9700\u8981\u5bf9\u5b9e\u4f8b\u505a\u76f8\u5bf9\u8f83\u5927\u7684\u4e00\u6b21\u53d8\u52a8\u3002<\/p>\n

\uff081\uff09\u65b0\u5efa\u7528\u6237\u63a7\u4ef6MyUserControl\uff0cMyUserControl\u4e0d\u5b8c\u6210\u4efb\u4f55\u6709\u610f\u4e49\u7684\u5de5\u4f5c\uff0cMyUserControl\u63a7\u4ef6\u7684XAML\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n

<UserControl x:Class=\"WpfRoutedEventExp.MyUserControl\" xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\" xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\" xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\" xmlns:d=\"http:\/\/schemas.microsoft.com\/expression\/blend\/2008\" mc:Ignorable=\"d\" d:DesignHeight=\"300\" d:DesignWidth=\"300\"> <Grid x:Name=\"Grid_FirstLayerInUserControl\"> <Grid x:Name=\"Grid_SecondLayerInUserControl\"> <Button x:Name=\"Button_ThirdLayerInUserControl\" Width=\"100\" Height=\"100\" Content=\"Hello,Kitty!\"\/> <\/Grid> <\/Grid> <\/UserControl><\/code><\/pre>\n<\/p>\n

\u4ee3\u7801\u5bf9\u5e94\u7684\u8bbe\u8ba1\u89c6\u56fe\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n

\"\u8def\u7531 <\/p>\n

\u56fe4 \u81ea\u5b9a\u4e49\u63a7\u4ef6\u7684\u8bbe\u8ba1\u56fe<\/strong><\/p>\n

\uff082\uff09\u5728\u5b9e\u4f8b\u4ee3\u7801\u4e2d\u4f7f\u7528\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u7528\u6237\u63a7\u4ef6\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u540d\u79f0\u4e3amyUserControl\u3002<\/p>\n<\/p>\n

<Grid x:Name=\"Grid_ThirdLayer\" > <local:DetailReportButton x:Name=\"Button_Confirm\" Width=\"100\" Height=\"100\" Content=\"Click Me\" Margin=\"142,111,250,100\" \/> <local:MyUserControl x:Name=\"myUserControl\" Width=\"100\" Height=\"100\" HorizontalAlignment=\"Left\" Margin=\"285,110,0,100\"\/> <\/Grid><\/code><\/pre>\n

\u81ea\u5b9a\u4e49\u63a7\u4ef6myUserControl\u4f4d\u4e8e\u5bb9\u5668Grid_ThirdLayer\u4e4b\u4e2d\uff0cButton_Confirm\u6309\u94ae\u4e4b\u540e\u3002<\/span> <\/p>\n

\"\u8def\u7531 <\/p>\n

\u56fe5 \u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u63a7\u4ef6<\/strong><\/p>\n

\uff082\uff09\u53bb\u6389\u6240\u6709\u7684\u4e8b\u4ef6\u76d1\u542c\u3002<\/p>\n

\uff083\uff09\u4f7fGrid_FirstLayer\u76d1\u542cButton\u7684Click\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n

this.Grid_FirstLayer.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Clicked1));<\/code><\/pre>\n<\/p>\n

\uff084\uff09\u4fee\u6539\u4e8b\u4ef6\u5904\u7406\u5668\u65b9\u6cd5Button_Clicked1\uff0c\u5728\u65b9\u6cd5\u4e2d\u8f93\u51fa\u4e8b\u4ef6\u7684Source\u4e0eOriginalSource\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n

private void Button_Clicked1(object sender, RoutedEventArgs e) { MessageBox.Show(e.Source + \";\" + e.OriginalSource ); }<\/code><\/pre>\n

\uff085\uff09<\/span>
\n\u70b9\u51fb\u201cHello\uff0cKitty!\u201d\u6309\u94ae\uff08\u201cHello\uff0cKitty!\u201d\u6309\u94ae\u4f4d\u4e8e\u81ea\u5b9a\u4e49\u63a7\u4ef6myUserControl\u4e2d\uff09\uff0c\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c:<\/span> <\/p>\n

\"\u8def\u7531 <\/p>\n

\u56fe6 Source\u4e0eOriginalSource\u7684\u533a\u522b<\/strong><\/p>\n

\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff1ae.Source\u7684\u503c\u4e3aMyUserControl\uff08\u5373\u81ea\u5b9a\u4e49\u63a7\u4ef6\uff09\uff0c\u800ce.OriginalSource\u7684\u503c\u4e3aButton\uff08\u5373\u201cHello\uff0cKitty!\u201d\u6309\u94ae\uff09\u3002<\/p>\n

Source\u4e0eOriginalSource\u5747\u4e3a\u4e8b\u4ef6\u6e90\u3002\u53ea\u4e0d\u8fc7Source\u8868\u793a\u7684\u662fLogicalTree\u4e0a\u7684\u6d88\u606f\u6e90\u5934\uff0c\u800cOriginalSource\u8868\u793a\u7684\u662fVisualTree\u4e0a\u7684\u6e90\u5934\u3002<\/strong><\/p>\n

Button.Click\u8def\u7531\u4e8b\u4ef6\u662f\u4ece\u81ea\u5b9a\u4e49\u63a7\u4ef6\u5185\u90e8\u540d\u4e3aButton_ThirdLayerInUserControl\u7684Button\u63a7\u4ef6\u53d1\u51fa\u7684\uff0c\u5728\u4e3b\u7a97\u53e3\u4e2d\uff0cmyUserControl\u662fLogicalTree\u7684\u672b\u7aef\u8282\u70b9\uff0c\u6240\u4ee5e.Source\u5c31\u662fMyUserControl\uff1b\u800c\u7a97\u4f53\u7684VisualTree\u5219\u5305\u542b\u4e86MyUserControl\u7684\u5185\u90e8\u7ed3\u6784\uff0c\u6240\u4ee5e.OriginalSource\u5c06\u8fd4\u56deButton_ThirdLayerInUserControl\u6309\u94ae\u3002<\/p>\n

<\/p>\n

\u5199\u5728\u540e\u9762<\/strong><\/p>\n

\u672c\u6587\u672a\u5b8c\uff0c\u4ee5\u540e\u5f85\u7eed...<\/p>\n","protected":false},"excerpt":{"rendered":"\u8def\u7531 wps \u6309\u94ae \u4f5c\u7528_\u8def\u7531\u5668\u51b2\u7a81\u600e\u4e48\u89e3\u51b3\u5199\u5728\u524d\u9762\u672c\u6587\u4e00\u5f00\u59cb\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u4f7f\u7528WPF\u8def\u7531\u4e8b\u4ef6\u7684\u5b9e\u4f8b\uff0c\u56e0\u4e3a\u672c\u6587\u6240\u6709\u7684\u8868\u8ff0\u90fd\u5c06\u57fa\u4e8e\u8be5\u5b9e\u4f8b","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5328"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5328"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5328\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}