Example #1
0
        /// <summary>
        /// Handle the Loaded event of the element to which this view model is attached
        /// inorder to enable the attached
        /// view model to bind to properties of the parent element
        /// </summary>
        static void Element_Loaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement targetElement = sender as FrameworkElement;
            FrameworkElement parent        = targetElement.Parent as FrameworkElement;

            // use the attached view model as the DataContext of the element it is attached to
            AttachedViewModelBase attachedModel = GetAttach(targetElement);

            targetElement.DataContext = attachedModel;

            // find the ProgressBar and associated it with the view model
            var progressBar = targetElement.Ancestors <ProgressBar>().Single() as ProgressBar;

            attachedModel.SetProgressBar(progressBar);

            // bind the DataContext of the view model to the DataContext of the parent.
            attachedModel.SetBinding(AttachedViewModelBase.DataContextProperty,
                                     new Binding("DataContext")
            {
                Source = parent
            });

            // bind the piggyback to give DataContext change notification
            attachedModel.SetBinding(AttachedViewModelBase.DataContextPiggyBackProperty,
                                     new Binding("DataContext")
            {
                Source = parent
            });
        }
Example #2
0
        /// <summary>
        /// Change handler for the Attach property
        /// </summary>
        private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement      targetElement = d as FrameworkElement;
            AttachedViewModelBase viewModel     = e.NewValue as AttachedViewModelBase;

            viewModel.AttachedElement = targetElement;

            // handle the loaded event
            targetElement.Loaded += new RoutedEventHandler(Element_Loaded);
        }
Example #3
0
        /// <summary>
        /// Handles changes to the DataContextPiggyBack property.
        /// </summary>
        private static void OnDataContextPiggyBackChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AttachedViewModelBase viewModel = d as AttachedViewModelBase;

            viewModel.OnDataContextChanged();
        }
Example #4
0
 public static void SetAttach(DependencyObject d, AttachedViewModelBase value)
 {
     d.SetValue(AttachProperty, value);
 }
 public static void SetAttach(DependencyObject d, AttachedViewModelBase value)
 {
     d.SetValue(AttachProperty, value);
 }