WPF 内部:为什么 UserControl 会覆盖 AdjustBranchSource?

发布于 2024-07-14 15:28:45 字数 304 浏览 3 评论 0原文

通过使用反射器,您可以看到WPF UserControl正在覆盖AdjustBranchSource。

    internal override void AdjustBranchSource(RoutedEventArgs e)
{

   e.Source = this;

}

我自己的问题是由该问题引起的不一致。 当元素位于用户控件内部或外部时。 Source 参数的行为有所不同。 令我惊讶的是,源应该始终是 RoutedEvent 目标中的元素。

问题是为什么要这样实施?

By Using reflector you can see that WPF UserControl is overriding AdjustBranchSource.

    internal override void AdjustBranchSource(RoutedEventArgs e)
{

   e.Source = this;

}

My very own problem regards inconsistency caused by that issue.
When an element is based inside a user control or outside. The Source parameter behaves differently. Which surprises me the source should always be the element in target by the RoutedEvent.

The question is why was it implemented like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-07-21 15:28:46

这有点道理。 如果您将 UserControl 视为黑匣子,那么您不应该知道其上有哪些控件,从而也不知道事件的来源。

如果您需要区分 UserControl 上的不同按钮,则 UserControl 应该有自己的按钮触发的事件。 这样,从外部看,它看起来像是正确的事件,并且 UserControl 的用户不需要知道哪个按钮执行了哪个事件。

举个例子,在列表框中,您是否需要知道向下滚动按钮是发送原始事件的按钮? 或者您只需要知道触发了向下滚动事件。

This kinda makes sense. If you treat the UserControl as a black box then you shouldn't know what controls are on it, and thus the source of an event.

If you need to distinguish between different buttons on the UserControl then the UserControl should have it's own events which the buttons trigger. That way from the outside it looks like the right event and the user of the UserControl doesn't need to know which button did which event.

To give an example, on a listbox, do you need to know that the down-scroll button was the button that sent the original event? Or do you just need to know that a scroll-down event was triggered.

夜血缘 2024-07-21 15:28:46

路由事件的源可以在事件路由的整个过程中发生变化。 我不完全确定为什么 UserControl 会更改它,但是您不能只使用 RoatedEventArgs 而是?

The source of a routed event can change throughout the routing of the event. I'm not entirely sure why UserControl changes it, but can you not just use the OriginalSource property on RoutedEventArgs instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文