Principales respuestas

TabControl Selection Change fires every time

Pregunta

Hi , Im Having wpf tabcontrol from my xaml page.Im using wpf mvp (CAL) for my app.according to tht my tabcontrol acts like host within tht i will be placing XAML files.TabselecctionChange fire whenver i click on button or wpf datagrid selection or combobox selection. reason behind firing tabselection change is ,since combobox also have selctionchange event .we have to set e.handled=true ,which solves tabcontrol firing ..but i cud not understand why we have to set e.handled =true. Is there any any common place to set e.handled=true without writing e.handled =true in every selectionchange of every control.Mahender

Respuestas

Although you handle the RoutedEvent in the child control, but that RoutedEvent still bubbles up in the visual tree till to the root. This is determined by the mechanism of Routed Event. But there is a important point to know is that even if you set e.Handled = true in the event handled for the child control, your parent control(directly or indirectly)still have chance to handle the routed event by settign the handledEventsToo parameter to true.

UIElement class exposes a AddHandler method to provide the functionality for elements to add the event handler in the code behind. AddHandler method has two overloads, one of which has a parameter named handledEventsToo, which means that even if you set e.Handled = true in some child elements in the visual tree. The parent control also can catch the routed event and handle it.

-->is there any way to keep e.Handled =true in a common place.since Im deriving custom tab control from wpf tabcontrol.

As far as I know, there is not a common way to place the e.Handled =true, but you can set the Handled argument in the lowest control’s event handler in the visual tree. So any other listener in the parent control will on longer to implement the handler.

ComboBox and TabControl are derived from Selector, and SelectionChanged event is a routed event, so the child ComboBox' SelectionChanged will be routed to parent control TabControl. This is the WPF routed event behaviour. Routed event bubble routing is accroding to the logical tree, if you put a ComboBox in a TabItem of a TabControl, when the ComboBox.SelectionChanged event raised, the event will be routed to the TabControl. But, if the ComboBox is not in the logical tree of TabControl, then the event will not be routed to the TabControl.

Todas las respuestas

ComboBox and TabControl are derived from Selector, and SelectionChanged event is a routed event, so the child ComboBox' SelectionChanged will be routed to parent control TabControl. This is the WPF routed event behaviour. Routed event bubble routing is accroding to the logical tree, if you put a ComboBox in a TabItem of a TabControl, when the ComboBox.SelectionChanged event raised, the event will be routed to the TabControl. But, if the ComboBox is not in the logical tree of TabControl, then the event will not be routed to the TabControl.

Since I have handled selectedchanged event for combobox why routed event gets bubbled up....is there any way to keep e.Handled =true in a common place.since Im deriving custom tab control from wpf tabcontrol.

Although you handle the RoutedEvent in the child control, but that RoutedEvent still bubbles up in the visual tree till to the root. This is determined by the mechanism of Routed Event. But there is a important point to know is that even if you set e.Handled = true in the event handled for the child control, your parent control(directly or indirectly)still have chance to handle the routed event by settign the handledEventsToo parameter to true.

UIElement class exposes a AddHandler method to provide the functionality for elements to add the event handler in the code behind. AddHandler method has two overloads, one of which has a parameter named handledEventsToo, which means that even if you set e.Handled = true in some child elements in the visual tree. The parent control also can catch the routed event and handle it.

-->is there any way to keep e.Handled =true in a common place.since Im deriving custom tab control from wpf tabcontrol.

As far as I know, there is not a common way to place the e.Handled =true, but you can set the Handled argument in the lowest control’s event handler in the visual tree. So any other listener in the parent control will on longer to implement the handler.