WPF:仅处理来自一个特定内容的冒泡事件

发布于 2024-09-08 15:26:34 字数 1840 浏览 1 评论 0原文

现在我要学习 RoutedEvents。我了解冒泡和隧道效应等等。但我有一个小问题,我不知道如何处理。

我在自己定义的 UserControl (继承 TabItem )中有一个 ItemsControl 。这个 itemsControl 可以是一个 ListBox 或一些尚未决定的选择器(取决于这个解决方案的最佳选择)。

现在,当选择一项时(还不确定最好的选择方法是什么,也许是双击),我需要捕获保存 UserControl 的 TabControl 中的事件。值得庆幸的是,当我的 userControl 被放入 XAML 视觉树中时,冒泡事件就会向上流动,并且事件可以到达 TabControl。然而,我在实现这一点时仍然遇到一些问题。

1)假设我将其设置为 MouseDoubleClick 我不想要其他 DoubleClick(就像在其他选项卡中或在 ItemsControlItems 之外)。我想你总是可以检查事件来自哪里,以确保它是正确的,但我不认为这是正确的方式,而且可能会带来麻烦。

2)如果我将事件设置为 SelectionChanged 并将 ItemsControl 设置为 ListBox,我必须确保 ListBox 本身不会处理它,以便它可以到达 tabControl。我该怎么做呢?

3)与2)相同,但TabControl本身就是一个选择器,因此它会引发它自己的selectionchange事件,不是吗?那会怎样呢?处理程序会同时从列表框和 TabControl 中抓取吗?

4)不确定我应该使用什么 ItemsControl。如果我只是用它来双击该项目,然后处理该事件,ListBox 就不会太多了。它有许多不受欢迎的影响,例如选择等。是定义自己的 ItemsControl 还是使用 ListBox 并覆盖它的一些属性(例如选择)更好(如何做到这一点?)?

5)事件的来源是 ItemsContainer,但我不需要项目本身。有一种简单的方法可以访问它,不是吗?

6) 假设我放置了一个按钮,在 DataTemplate 中我不想通过按钮附加对象的 ID,并在处理按钮单击的位置捕获它以识别它是什么项目。是否有一个属性可以做到这一点,或者我必须自己定义它?

7) 最好的做法可能是创建我自己的活动吗?

感谢您对此问题的看法以及对此处最好采取什么流程的评论。

编辑:决定根据要求添加更多信息。

我的目标是什么;当我启动程序时,我有一个 TabControl,其中有 1 个选项卡。该选项卡与其他选项卡不同,因为它包含对象列表(CompanyModel)。这个列表就是我提到的ItemsControl。当您按下此列表中的一家公司时,会添加一个新选项卡,其中包含有关该公司的信息。

我的解决方案是创建一个 TabItem 集合作为 TabControl 的源。然后,我将在单独的 XAML 源文件中定义 2 种类型的 TabItem(不知道如何称呼它,基本上是添加新的用户控件)。一个是列表出现的初始位置,另一个接受 CompanyModel 作为构造函数参数并显示有关该信息的信息。

这是可行的,虽然我不喜欢单独定义公司列表 tabitem 的想法,但我无法在 tabcontrol 下定义它,因为我只能定义项目或来源,而不是两者。

我尝试过这个想法,方法是在 List DataTemplate 上放置一个按钮,将所选公司的 Id 冒泡到 TabControl,在那里处理它并使用 CompanyModel 添加一个新的 TabItem。这只是一个尝试的解决方案,并且存在一些问题,例如如果我向 CompanyTabs 添加另一个按钮,我也会在 TabControl 中抓取它们。

因此,我的问题可以通过找到一种从原始 listTab 中冒泡事件的好方法来解决,或者如果我能够在与 TabControl 相同的位置定义选项卡。

我知道另一种解决方案是将列表与 TabControl 分开,但其他人想看看这是如何实现的,我想了解如何做到这一点。

Now I'm about to learn RoutedEvents. I understand bubbling and tunneling and all. But I have a little problem I'm not sure how to handle.

I've got a ItemsControl in my own defined UserControl ( inherits TabItem ). This itemsControl could be a ListBox or some selector not decided yet ( depends what is best with this solution ).

Now when one item there is selected ( Not sure yet what the best selection method would be, perhaps doubleclick ) I need to capture the event in the TabControl that holds my UserControl. Thankfully as my userControl is put into the XAML visualtree the bubbling event flows up and events can reach the TabControl. However I still have a few problems with implementing this.

1) Let's say I set it as MouseDoubleClick I don't wan't other DoubleClicks ( like in other tabs or outside the ItemsControlItems ). I guess you can always do a check where the events coming from to ensure it's the right one but I don't feel it's the right way and it could be asking for trouble.

2) If I set the event to SelectionChanged and the ItemsControl to ListBox I would have to ensure the ListBox itself doesn't handle it so it can reach the tabControl. How would I do that?

3) Same as 2) but the TabControl is a selector itself so it would raise it's own selectionchange events wouldn't it. how would that work. Would the handler grab both from the listbox and the TabControl?

4) Not sure what ItemsControl I should use. If i'm only using it to doubleclick the item and then handle the event wouldn't ListBox be too much. It has many effects that would be undisireable like selection and such. Is it better to define your own ItemsControl or maybe use ListBox and overwrite some of it's properties like selection ( how would one do that? )?

5) The source of the event would be the ItemsContainer but I wan't the item itself. There is a easy way to get access to it isn't there?

6) Let's say I put a button and in the DataTemplate I wan't to attach the ID of the object through the button and capture it where the button click is handled to identify what item it was. Is there a property do do that or would I have to define it myself?

7) Would the best course of action here maybe to create my own event?

Your take on this issue and comments on what process is best to take here is appriciated.

EDIT: Decided to add little bit more info as asked.

What I'm aiming for; when I start the program I have a TabControl with 1 tab in it. This tab is different from the rest of the tabs will be because it holds a list of objects (CompanyModel). This list is the ItemsControl I mention. When you press one company from this list a new tab is added with info about that company.

My solution was to create a TabItem collection as a source for the TabControl. Then I would define my 2 types of TabItems in a seperate XAML source file (not sure what to call it, basicly add new usercontrol). One is the initial where thhe list appears, the other one takes in a CompanyModel as a constructor parameter and shows info about that.

This works while I don't like the idea of defining the company list tabitem seperatly I can't define it under the tabcontrol because I can only have defined items or sourced, not both.

I've tried this idea by putting a button on the List DataTemplate that bubbles the Id of the company selected up to the TabControl, handling it there and adding a new TabItem with the CompanyModel. That was just a solution to try it and has problems like if I add anpther button to the CompanyTabs I would grab them aswell in the TabControl.

So my problem could be solved by finding a good way to bubble a event from the original listTab or If I would be able to define the tab in the same place as the TabControl.

I know that one other solution would to have the list seperate from the TabControl but others would like to see how this comes out and I would like to learn how I would do this.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文