none
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
    domingo, 19 de abril de 2009 17:27

Respuestas

  • HI,

     

    -->Since I have handled selectedchanged event for combobox why routed event gets bubbled up....

     

    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.

     

    For more information about the mechanism ,pleae refer this MSDN document:
    http://msdn.microsoft.com/en-us/library/ms742806.aspx

     

    The following is a small example to illustrate the concept of handledEventsToo.
       <StackPanel>

            <TabControl Name="tabControl" >

                <TabItem Header="Header" >

                    <ComboBox SelectionChanged="OnComboxBoxChanged">

                        <TextBlock Text="Item1" />

                        <TextBlock Text="Item1" />

                        <TextBlock Text="Item1" />

                    </ComboBox>

                </TabItem>

            </TabControl>

    </StackPanel>

     

    In the code behind:

       public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

                this.tabControl.AddHandler(TabControl.SelectionChangedEvent, new SelectionChangedEventHandler(OnTabControlChanged),true   );

            }

            private void OnTabControlChanged(object sender, SelectionChangedEventArgs e)

            {

                //even if the routed event is handled by ComboBox, this handler still can run.

            }

            private void OnComboxBoxChanged(object sender, SelectionChangedEventArgs e)

            {

                e.Handled = true;

            }

        }

     

    -->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.

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    • Marcado como respuesta Jim Zhou - MSFT viernes, 24 de abril de 2009 12:34
    viernes, 24 de abril de 2009 8:28
  • Hi

    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.

    Hope this helps.


    Yiling, MVP(Visual C++)
    • Marcado como respuesta Jim Zhou - MSFT viernes, 24 de abril de 2009 12:34
    lunes, 20 de abril de 2009 1:39

Todas las respuestas

  • Hi

    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.

    Hope this helps.


    Yiling, MVP(Visual C++)
    • Marcado como respuesta Jim Zhou - MSFT viernes, 24 de abril de 2009 12:34
    lunes, 20 de abril de 2009 1:39
  • HI Lai,

    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.

    Mahender
    martes, 21 de abril de 2009 11:33
  • HI,

     

    -->Since I have handled selectedchanged event for combobox why routed event gets bubbled up....

     

    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.

     

    For more information about the mechanism ,pleae refer this MSDN document:
    http://msdn.microsoft.com/en-us/library/ms742806.aspx

     

    The following is a small example to illustrate the concept of handledEventsToo.
       <StackPanel>

            <TabControl Name="tabControl" >

                <TabItem Header="Header" >

                    <ComboBox SelectionChanged="OnComboxBoxChanged">

                        <TextBlock Text="Item1" />

                        <TextBlock Text="Item1" />

                        <TextBlock Text="Item1" />

                    </ComboBox>

                </TabItem>

            </TabControl>

    </StackPanel>

     

    In the code behind:

       public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

                this.tabControl.AddHandler(TabControl.SelectionChangedEvent, new SelectionChangedEventHandler(OnTabControlChanged),true   );

            }

            private void OnTabControlChanged(object sender, SelectionChangedEventArgs e)

            {

                //even if the routed event is handled by ComboBox, this handler still can run.

            }

            private void OnComboxBoxChanged(object sender, SelectionChangedEventArgs e)

            {

                e.Handled = true;

            }

        }

     

    -->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.

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    • Marcado como respuesta Jim Zhou - MSFT viernes, 24 de abril de 2009 12:34
    viernes, 24 de abril de 2009 8:28