Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

C#
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
C#
cmb1.SelectedIndex = -1;
after
C#
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 :

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



I also try to solve this problem using a ViewModel. and bind my Selected index.

here is my MVVM code.

MainWindow.xaml:

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



Here is the ViewModel code in C# :

C#
public class ViewModel:INotifyPropertyChanged
        {
            public ViewModel()
            {
                List = new ObservableCollection<Model>();
                List.Add(new Model() { Name = "itme1" });
                List.Add(new Model() { Name = "itme2" });
                List.Add(new Model() { Name = "itme3" });
            }
            public ObservableCollection<Model> List { get; set; }
           
            private int _selected;
            public int Selected
            {
                get { return _selected; }
                set
                {
                    int temp = _selected;
                    _selected = value;
                    _previousSelected = temp;
                    NotifyPropertyChanged("Selected", temp, value);
                    NotifyPropertyChanged("PreviousSelected", temp, temp);
                    Window1 win1 = new Window1();
    
    
                    if (_selected ==List.Count - 1)
                    {
    
                        win1.Show();
    
                        if (_selected == _previousSelected)
                        {
                            win1.Close();
                        }
                    }
                    else
                    {
                        win1.Close();
                    }
                }
            }
            int _previousSelected = 0;
            public int PreviousSelected
            {
                get { return _previousSelected; }
    
            }
            public event PropertyChangedEventHandler PropertyChanged;
            protected void NotifyPropertyChanged<T>(string propertyName, T oldvalue, T newvalue)
            {
                OnPropertyChanged(this, new PropertyChangedExtendedEventArgs<T>(propertyName, oldvalue, newvalue));
            }
            public virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(sender, e);
            }
        }
        public class Model
        {
            public string Name { get; set; }
        }
        public class PropertyChangedExtendedEventArgs<T> : PropertyChangedEventArgs
        {
            public virtual T OldValue { get; private set; }
            public virtual T NewValue { get; private set; }
    
            public PropertyChangedExtendedEventArgs(string propertyName, T oldValue, T newValue)
                : base(propertyName)
            {
                OldValue = oldValue;
                NewValue = newValue;
            }
        }


What I have tried:

I try to solve this problem both using code behind and also using a ViewModel but the problem remains same. Our Goal is to give a click event to the ComboBox items show that the window will appear again and again if I click on the last ComboBox item without clicking another item.
Posted
v2
Comments
Member 15627495 22-Oct-22 5:03am    
the event choosen seems to be a wrong one,
instead of 'onchange event' , why don't you use 'on click event' , with a little timer delay to avoid 'intermediate click' and the click for the final choice of the user.

one other way, is to add an event handler , keep 'onchange' , and permit to 'on click' to be used.
Member 15061773 22-Oct-22 5:12am    
I also try to use the PreviewMouse Down event but the result is same. I don't want to use any timer delay that's why I use MVVM pattern. If you have any code solution then please tell me. I am in great trouble.
Member 15627495 22-Oct-22 5:41am    
why don't you add a button , aside the combo box ???
Member 15061773 22-Oct-22 5:54am    
Can you have some demo code because it's too much complex. Give your answer with proper code and I definitely accept your answer.
Graeme_Grant 22-Oct-22 8:09am    
When hitting these types of issues, sometimes it is a good idea to create a test/prototype project.

I would create a new project, add a combobox to a form with test data, then wire up events and use the codebehind. Then you can try each event until you find the one that works for your need. This is called process of elimination. Then you are ready to implement in your main project. This is what I would need to do to identify a working solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900