I have a ObservableCollection which is bound to a tabcontrol
.
<TabControl
ItemsSource="{Binding Steps}"
SelectedItem="{Binding SelectedStep}"
CanUserReorder="{Binding EditMode}"
</TabControl>
The user can reorder the tab items, if he is in EditMode. When I am leaving the view with the tabcontrol, I lose the tab order, which was modified by the user. How can I prevent this?
Thanks for your help!
I got it! This solution is implemented in mvvm-pattern.
Every time the collection Steps is changing add or remove the items and afterwards reorder their positions. The TabItems of the TabControl in the View could be reordered by the user through using TabControl of ComponentOne or by using Bea Stollnitz's Drag/Drop functionality.
ViewModel:
public class MyViewModel
{
public MyModel Model {/*get... set*/}
public ObservableCollection<StepViewModel> Steps {/*get... set...*/}
public StepViewModel SelectedStep {/*get... set...*/}
public MyViewModel()
{
Model = new MyModel();
Steps = new ObservableCollection<StepViewModel>();
Model.Steps = new ObservableCollection<StepModel>();
// Load the saved Model with the steps and their positions
Model.Steps = LoadFromXml();
Steps.CollectionChanged += Steps_OnCollectionChanged;
// Add the Steps in the right order
for (int i = 0; i < Steps.Count; i++)
{
var item = Steps.First(x => x.Position == i);
var vm = new StepViewModel();
Steps.Add(vm);
}
}
private void Steps_OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (StepViewModel vm in e.NewItems)
{
if (!Model.Steps.Contains(vm.Model))
Model.Steps.Add(vm.Model);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (StepViewModel vm in e.OldItems)
{
if (Model.Steps.Contains(vm.Model))
Model.Steps.Remove(vm.Model);
}
}
// Match the temporary collectionindex to a position-property
foreach (StepViewModel item in Steps)
{
item.Position = Steps.IndexOf(item);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.