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>
I also try to solve this problem using a ViewModel. and bind my Selected index.
here is my MVVM code.
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>
Here is the ViewModel code in 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.