locked
Differentiate source of SelectionChanged event of ComboBox

    Question

  • I have a XAML ComboBox element in the UI and have registered an event handler for its SelectionChanged event. In my app, the ComboBox selection is changed either by users' manipulation or programmatically. I want the event handler code to be executed only in the former case, how can I do this?
    Monday, November 19, 2012 2:15 PM

All replies

  • Hello,

    Would you please provide us the whole codes to reproduce this issue,

    You can upload your project to skydriver
    http://skydrive.live.com/

    It is not necessary that you send out the whole of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business details from it.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 22, 2012 8:49 AM
  • Jesse, I think I can describe my issue with just a couple lines of code. In a XAML page, I define a ComboBox control

    <ComboBox x:Name="DerivedElements" SelectionChanged="OnDerivedElementsSelectionChanged" Width="161" Margin="6,0,0,0" Height="35"/>
    

    The event handler OnDerivedElementsSelectionChanged will do some work when users change the current selection. Everything is find so far.

    The issue arises when I initialzed this ComboBox control in the NavigatedTo function

    // Provide a default selection, or no items would be drawn in the ComboBox until users select one.
    DerivedElements->SelectedIndex = 0;
    
    This assignment statement will make event handler OnDerivedElementsSelectionChanged to be called, which is undesirable.
    Thursday, November 22, 2012 4:23 PM
  • Hi,

    I cannot reproduce your issue with these codes.

           <ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="318,154,0,0" VerticalAlignment="Top" Width="250" Height="102" SelectionChanged="ComboBox1_SelectionChanged_1">
                <x:String>Item1</x:String>
                <x:String>Item2</x:String>
                <x:String>Item3</x:String>
                <x:String>Item4</x:String>
            </ComboBox>
    void App1::MainPage::ComboBox1_SelectionChanged_1(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
    {
    	int a=5;
    }

    Please upload a sample code to skydriver

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 23, 2012 8:40 AM
  • You may add a statement like

    ComboBox1->SelectedIndex = 0;
    and see your event handler ComboBox1_SelectionChanged_1 is called.
    Friday, November 23, 2012 2:55 PM
  • I have exactly the same problem in my app, whenever there is a handler attached to a property/event then while setting it from code, this handler is executed.
    Friday, November 23, 2012 7:22 PM
  • I have added this line in OnNavigatedTo function, but still cannot reproduce.

    Please upload a sample code to skydriver.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 26, 2012 9:24 AM