开发者

WPF: Handling bubbling events only from one specific content

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 CompanyMod开发者_运维百科el 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.


please have a look in this blog, may be it will helps u. http://blogs.u2u.be/diederik/post/2009/09/09/In-WPF-SelectionChanged-does-not-mean-that-the-selection-changed.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜