Last active
August 29, 2015 14:17
-
-
Save nickdarnell/c5b3d4b593bef1666228 to your computer and use it in GitHub Desktop.
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 static class ViewModelBehavior | |
| { | |
| public static readonly DependencyProperty LoadUnloadProperty = | |
| DependencyProperty.RegisterAttached("LoadUnload", typeof(Boolean), | |
| typeof(ViewModelBehavior), | |
| new FrameworkPropertyMetadata(false, | |
| new PropertyChangedCallback(OnLoadUnloadChanged))); | |
| public static void SetLoadUnload(FrameworkElement element, Boolean value) | |
| { | |
| element.SetValue(LoadUnloadProperty, value); | |
| } | |
| public static Boolean GetLoadUnload(FrameworkElement element) | |
| { | |
| return (Boolean)element.GetValue(LoadUnloadProperty); | |
| } | |
| public static void OnLoadUnloadChanged(DependencyObject obj, | |
| DependencyPropertyChangedEventArgs args) | |
| { | |
| FrameworkElement element = obj as FrameworkElement; | |
| if (element == null) | |
| throw new InvalidOperationException(); | |
| element.DataContextChanged += (sender, e) => | |
| { | |
| if (!element.IsLoaded) | |
| return; | |
| if (e.OldValue is IViewModel) | |
| { | |
| IViewModel viewModel = ((IViewModel)e.OldValue); | |
| if (viewModel.Initialized) | |
| { | |
| viewModel.Unload(element); | |
| viewModel.Initialized = false; | |
| } | |
| } | |
| if (e.NewValue is IViewModel) | |
| { | |
| IViewModel viewModel = ((IViewModel)e.NewValue); | |
| if (!viewModel.Initialized) | |
| { | |
| viewModel.Initialized = true; | |
| viewModel.Load(element); | |
| } | |
| } | |
| }; | |
| element.Loaded += (sender, e) => | |
| { | |
| IViewModel viewModel = | |
| element.GetValue(FrameworkElement.DataContextProperty) as IViewModel; | |
| if (viewModel != null && !viewModel.Initialized) | |
| { | |
| viewModel.Initialized = true; | |
| viewModel.Load(element); | |
| } | |
| }; | |
| element.Unloaded += (sender, e) => | |
| { | |
| IViewModel viewModel = | |
| element.GetValue(FrameworkElement.DataContextProperty) as IViewModel; | |
| if (viewModel != null && viewModel.Initialized) | |
| { | |
| viewModel.Unload(element); | |
| viewModel.Initialized = false; | |
| } | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment