Created
November 3, 2011 15:51
-
-
Save FelicePollano/1336844 to your computer and use it in GitHub Desktop.
Avalon TabModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ad:DockingManager Grid.Row="1"> | |
| <ad:ResizingPanel> | |
| <ad:DocumentPane b:UseConductor.DocumentConductor="{Binding}"/> | |
| </ad:ResizingPanel> | |
| </ad:DockingManager> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainViewModel:Conductor<IScreen>.Collection.OneActive | |
| { | |
| public void Loaded() | |
| { | |
| ActivateItem(new TabModel() { DisplayName = "Example 1" }); | |
| ActivateItem(new TabModel() { DisplayName = "Example 2" }); | |
| ActivateItem(new TabModel() { DisplayName = "Example 3" }); | |
| ActivateItem(new TabModel() { DisplayName = "Example 4" }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //ispired from http://frankmao.com/2010/11/19/when-caliburn-micro-meets-avalondock/ | |
| public class TabModel:Screen | |
| { | |
| protected override void OnActivate() | |
| { | |
| base.OnActivate(); | |
| } | |
| protected override void OnDeactivate(bool close) | |
| { | |
| base.OnDeactivate(close); | |
| } | |
| public override void CanClose(Action<bool> callback) | |
| { | |
| if (MessageBox.Show("Do you really want to close" + DisplayName, "Close ?", System.Windows.MessageBoxButton.OKCancel) == System.Windows.MessageBoxResult.OK) | |
| { | |
| callback(true); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UseConductor:DependencyObject | |
| { | |
| public static object GetDocumentConductor(DependencyObject obj) | |
| { | |
| return obj.GetValue(DocumentConductorProperty); | |
| } | |
| public static void SetDocumentConductor(DependencyObject obj, object value) | |
| { | |
| obj.SetValue(DocumentConductorProperty, value); | |
| } | |
| static Dictionary<DockingManager, ContentControl> previousActive = new Dictionary<DockingManager, ContentControl>(); | |
| public static readonly DependencyProperty DocumentConductorProperty = | |
| DependencyProperty.RegisterAttached("DocumentConductor", typeof(object), typeof(UseConductor), new UIPropertyMetadata(null, | |
| (depo, depa) => | |
| { | |
| if (depo is DocumentPane) | |
| { | |
| var pane = depo as DocumentPane; | |
| if (pane.GetManager() == null) | |
| return; | |
| pane.GetManager().ActiveDocumentChanged += (s, e) => | |
| { | |
| var dm = s as DockingManager; | |
| if (previousActive.ContainsKey(dm)) | |
| { | |
| var prev = ViewModelLocator.LocateForView(previousActive[dm].Content) as IScreen; | |
| if (null != prev) | |
| { | |
| prev.Deactivate(false); | |
| } | |
| } | |
| previousActive[dm] = pane.GetManager().ActiveDocument; | |
| var current = ViewModelLocator.LocateForView(pane.GetManager().ActiveDocument.Content) as IScreen; | |
| if (null != current) | |
| { | |
| current.Activate(); | |
| } | |
| }; | |
| var conductor = depa.NewValue as Conductor<IScreen>.Collection.OneActive; | |
| conductor.Items.CollectionChanged += (s, e) => | |
| { | |
| switch (e.Action) | |
| { | |
| case NotifyCollectionChangedAction.Add: | |
| foreach (var screen in e.NewItems) | |
| { | |
| var view = LocateViewFor(screen); | |
| var tabItem = new DocumentContent(); | |
| tabItem.Closing += (ss, ee) => | |
| { | |
| var closingScreen = screen as IScreen; | |
| if (null != closingScreen) | |
| { | |
| ee.Cancel = true; | |
| closingScreen.CanClose((close)=>ForceClose(pane,closingScreen,close,conductor) | |
| ); | |
| } | |
| }; | |
| ViewModelBinder.Bind(screen, tabItem, null); | |
| tabItem.Content = view; //TODO: can this be done by xaml caliburn.View | |
| BindTabTitle(tabItem); | |
| (depo as DocumentPane).Items.Add(tabItem); | |
| } | |
| break; | |
| case NotifyCollectionChangedAction.Remove: | |
| foreach (var screen in e.OldItems) | |
| { | |
| foreach (ContentControl doc in (depo as DocumentPane).Items) | |
| { | |
| if (doc.Content == screen) | |
| { | |
| (depo as DocumentPane).Items.Remove(doc); | |
| } | |
| } | |
| } | |
| break; | |
| case NotifyCollectionChangedAction.Reset: | |
| (depo as DocumentPane).Items.Clear(); | |
| break; | |
| } | |
| }; | |
| } | |
| } | |
| )); | |
| private static void ForceClose(DocumentPane pane,IScreen closingScreen,bool close,Conductor<IScreen>.Collection.OneActive conductor) | |
| { | |
| if (close == true) | |
| { | |
| foreach (var d in pane.Items) | |
| { | |
| var screen = ViewModelLocator.LocateForView(d); | |
| if (screen == closingScreen) | |
| { | |
| closingScreen.Deactivate(true); | |
| pane.Items.Remove(d); | |
| conductor.DeactivateItem(closingScreen,false); | |
| conductor.Items.Remove(closingScreen); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| private static object LocateViewFor(object viewModel) | |
| { | |
| var view = ViewLocator.LocateForModelType(viewModel.GetType(), null, null); | |
| return view; | |
| } | |
| static void BindTabTitle(DocumentContent tab) | |
| { | |
| DependencyProperty textProp = DocumentContent.TitleProperty; | |
| Binding b = new Binding("DisplayName"); | |
| BindingOperations.SetBinding(tab, textProp, b); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment