Hi,@MERUN KUMAR MAITY. For setting the second item selected previously becoming the last item, you could try to refer to the following code.
xaml:
<StackPanel>
<Button x:Name="btn" Click="btn_Click" Height="30" Content=" add item"/>
<ComboBox x:Name="cmb1" Width="200" Height="60" ItemsSource="{Binding Icv}" DisplayMemberPath="Name" SelectedItem="{Binding Mode=TwoWay,Path=SelectedItem}" />
</StackPanel>
Codebehind:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace ComboboxDemo
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public int flag = 2;
private ObservableCollection<AudioDevice> devices = new ObservableCollection<AudioDevice>();
public ObservableCollection<AudioDevice> Devices
{
get { return devices; }
set
{
if (devices != value)
{
devices = value;
OnPropertyChanged("Devices");
}
}
}
private AudioDevice m_selectedItem;
public AudioDevice SelectedItem
{
get { return m_selectedItem; }
set
{
if (m_selectedItem != value)
{
m_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
}
private ICollectionView icv;
public ICollectionView Icv
{
get { return icv; }
set
{
icv = value;
OnPropertyChanged("Icv");
}
}
public MainWindow()
{
InitializeComponent();
if (flag != null)
{
for (int i = 1; i < 5; i++)
{
Devices.Add(new AudioDevice() { Name = $"Name{i}", Direction = "Playback", Id = $"{i}", Default = true });
}
Icv = new ListCollectionView(Devices);
}
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void btn_Click(object sender, RoutedEventArgs e)
{
Devices.Insert( 0, new AudioDevice() { Name = $"Name{flag}...", Direction = "Recording", Id = $"{flag}", Default = false });
ObservableCollectionExtensions.Swap(Devices, SelectedItem, Devices.Last());
SelectedItem = Devices.Last();
flag++;
}
}
public static class ObservableCollectionExtensions
{
public static void Swap<T>(
this ObservableCollection<T> collection,
T obj1, T obj2)
{
if (!(collection.Contains(obj1) && collection.Contains(obj2))) return;
var indexes = new List<int>
{collection.IndexOf(obj1), collection.IndexOf(obj2)};
if (indexes[0] == indexes[1]) return;
indexes.Sort();
var values = new List<T> { collection[indexes[0]], collection[indexes[1]] };
collection.RemoveAt(indexes[1]);
collection.RemoveAt(indexes[0]);
collection.Insert(indexes[0], values[1]);
collection.Insert(indexes[1], values[0]);
}
}
public class AudioDevice
{
public string Name { get; set; }
public string Direction { get; set; }
public string Id { get; set; }
public bool Default { get; set; }
}
}
The result:
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.