事件来源与原始来源 [英] Event source vs original source

查看:93
本文介绍了事件来源与原始来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读C#WPF书籍,在路由事件一章中,事件具有2个相同的属性SourceOriginalSource.我没有看到它们之间的区别:

I'm reading C# WPF book and in routed events chapter, event has 2 same properties Source and OriginalSource. I did not see the difference between them:

Xaml:

<Button Name="Ok" Click="Ok_Click"/>

背后的代码:

private void Ok_Click(object sender, RoutedEventArgs e)
{
        bool flag = false;
        var source = e.Source;
        var originalSource = e.OriginalSource;
        if (source == originalSource)
        {
            flag = true;
        }
}

flag属性true在这里,有人可以解释为什么2个相同的属性,或者在这种情况下此属性无效吗?或在哪里可以看到该属性的用例?

and flag property true here, can some one explain why 2 same property or in this case this properties has no effects? or where can we see use case for this properties?

推荐答案

一个控件可以在其中包含其他控件作为子控件.当您从控件中订阅事件时,您所订阅的父对象很可能是e.Source,但是,如果控件中有子对象,而该子对象是引发该事件的子对象,则OriginalSource将是引发该事件的子对象.事件.

A control can have other controls within it as children. When you subscribe to a event from the control the parent you subscribed to is likely to be the e.Source however if the control has children and the child is the one that raised the event then the OriginalSource will be the child that raised the event.

一个常见的用例是订阅DataGrid上的="noreferrer"> KeyDown 事件.源可能是网格,但原始源可能是单元(或单元中的某些组件)

A common use case is subscribing to the KeyDown event on a DataGrid. The source may be the grid but the original source might be the cell (or some component in the cell)

这篇关于事件来源与原始来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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