How to trigger SelectionChanged event when ComboBox selected Index is same not necessarily changed in WPF C#?

MERUN KUMAR MAITY 551 Reputation points
2022-10-19T17:20:30.463+00:00

Hi there, I have a simple WPF application where a different window will open if I select the last item of a Combo box. my code is working successfully but there some issue on my code. I want some more functionality. Suppose, I select the last item, A window will appear. if I select the last item again then the another window will not appear again, that only works if I select another item after that again the last item.

I want to open that different window again and again by selecting the last item it does not matter if I select the last item, last time or not.

Here is my C# code in MainWindow.xaml.cs :

 private void cmb1_SelectionChanged(object sender, SelectionChangedEventArgs e) {  
      
       Window1 win1 = new Window1();  
      
       if (cmb1.SelectedIndex == cmb1.Items.Count - 1)  
      
       {  
         win1.Show();  
      
       } else {  
      
         win1.Close();  
      
       }  
      
     }  

Here I also tried by putting cmb1.SelectedIndex = -1; after win1.Show();. It works but it behaves abnormally that means my ComboBox became blank which I obviously don't want.

Here is my XAML code :

<Grid x:Name="Grid">  
<ComboBox x:Name="cmb1" Width="400" Height="25" SelectionChanged="cmb1_SelectionChanged" >  
<ComboBoxItem >item1</ComboBoxItem>  
<ComboBoxItem>item2</ComboBoxItem>  
<ComboBoxItem>item3</ComboBoxItem>  
<ComboBoxItem>item4</ComboBoxItem>  
</ComboBox>  
</Grid>  

My final approach is to Create a proper ViewModel and use DataBinding with simple properties instead. But I don't know How to do that in this scenario.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,716 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,656 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rafael Faria 0 Reputation points
    2024-07-29T16:18:49.29+00:00

    I know it's late, but have you tried "ComboBox.SelectedItems.Clear"?

    1 person found this answer helpful.

  2. Hui Liu-MSFT 47,421 Reputation points Microsoft Vendor
    2022-10-20T07:45:32.817+00:00

    Using the MVVM code below can achieve the same effect as yours.

    MainWindow.xaml:

     <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
        <StackPanel x:Name="Grid">  
             
            <ComboBox Name="cb2" Width="400" Height="30" ItemsSource="{Binding List}"  
                      DisplayMemberPath="Name"  
                SelectedValuePath="Name"  
                      SelectedIndex="{Binding Selected}">  
                 
            </ComboBox>  
        </StackPanel>  
    

    Codebehind:
    252381-4444.txt

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.