hmm, I donโt know if this is a โgoodโ way, but if you can access the selected item before rebooting, you can save it (or its key or something else) and select it again after the reboot is performed.
quick layout:
var selectedItem = myCombo.SelectedItem; DoReload(); myCombo.SelectedItem = selectedItem;
But I guess you mean a different way, how does this guide work?
Hope this helps anyway ...
UPDATE
Ok, I see from the background thread.
Do you use ICollectionView to bind dropdowns? If so, you can use the CurrentItem property to save the link. I made a quick layout and it works on my setup. this assumes you have a link to your interface:
Xaml
<Grid VerticalAlignment="Top"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <ComboBox ItemsSource="{Binding Items}" IsSynchronizedWithCurrentItem="True" Grid.Column="0" Grid.Row="0" DisplayMemberPath="Name"/> <Button Command="{Binding UpdateCommand}" Grid.Column="1" Grid.Row="0">Update</Button> </Grid>
View / ViewModel
public partial class Window1 : Window { public Window1() { InitializeComponent(); this.DataContext = new ViewModel(this); } } public class ViewModel { private readonly Window1 window; private ObservableCollection<Item> items; private ICollectionView view; public ViewModel(Window1 window) { this.window = window; items = new ObservableCollection<Item> { new Item("qwerty"), new Item("hello"), new Item("world"), }; view = CollectionViewSource.GetDefaultView(items); } public ObservableCollection<Item> Items { get { return items; } } public ICommand UpdateCommand { get { return new RelayCommand(DoUpdate); } } public Item SelectedItem { get; set; } private void DoUpdate(object obj) { var act = new Func<List<Item>>(DoUpdateAsync); act.BeginInvoke(CallBack, act); } private List<Item> DoUpdateAsync() { return new List<Item> { new Item("hello"), new Item("world"), new Item("qwerty"), }; } private void CallBack(IAsyncResult result) { try { var act = (Func<List<Item>>)result.AsyncState; var list = act.EndInvoke(result); window.Dispatcher.Invoke(new Action<List<Item>>(delegate(List<Item> lst) { var current = lst.Single(i => i.Name == ((Item)view.CurrentItem).Name); Items.Clear(); lst.ForEach(Items.Add); view.MoveCurrentTo(current); }), list); } catch(Exception exc){ Debug.WriteLine(exc); } } } public class Item { public Item(string name) { Name = name; } public string Name { get; set; } }
You will need to do some processing if the selected item is no longer listed.
The IsSynchronizedWithCurrentItem property is important here, otherwise it will not work!
In addition, the way the main window refers should be with a DI framework.
Roelf
source share