/// <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); }
/// <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; // bind the DataContext of the view model to the DataContext of the parent. attachedModel.SetBinding(RadialGaugeControlViewModel.DataContextProperty, new Binding("DataContext") { Source = parent }); // bind the piggyback to give DataContext change notification attachedModel.SetBinding(RadialGaugeControlViewModel.DataContextPiggyBackProperty, new Binding("DataContext") { Source = parent }); }
/// <summary> /// Handles changes to the DataContextPiggyBack property. /// </summary> private static void OnDataContextPiggyBackChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { AttachedViewModelBase viewModel = d as AttachedViewModelBase; viewModel.OnDataContextChanged(); }
public static void SetAttach(DependencyObject d, AttachedViewModelBase value) { d.SetValue(AttachProperty, value); }