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.
Hi@MERUN KUMAR MAITY .
It works but it behaves abnormally that means my ComboBox became blank which I obviously don't want.
Could you take a screenshot to show what the specific performance is?
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.
Thank you very much sir for your answer. As you always here to help me. But unfortunately your code is not working. Because according to my requirements the Window should be open if I select the last item again and again without selecting any other item. But after applying your code my issue remain shame.
Now. come to another Another question.
Could you take a screenshot to show what the specific performance is?
There is nothing to take screenshot, I can tell you in a single sentence. As soon as I apply the cmb1.SelectedIndex = -1;
after win1.Show()
. The window is opening if I Select the same index again and again but my ComboBox became blank. Though my ComboBox item are there as usual but there no ComboBox item highlight present.
This highlight is mainly tell me which Item I select previously. Though I attach some screenshot to understand the matter better.
Hi@MERUN KUMAR MAITY .
After testing your code, here is the result. (The first ComboBox uses your code and the second ComboBox uses my code.)
Click the last item to display Window1 and then click the last item again to no longer display Window1.
Click other items to display Window1 and then click the last item to display Window1.
Do you want the effect mentioned above?
My Combobox doesn't show blank. The effect of my code is as follows.
If not, please describe the problem in more detail.
Thank you very much sir, for your reply. You did not understand my question properly. Let's take your Combo Box in this case. As you shown your ComboBox has total three items. If I click the last item that means the item3, then a window will appear that's fine. But my requirement is I want to click the item3 again and the window should open again without clicking any other tabitem in this case tab item1, item2.
Hope you understand.