/// <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 }); }
/// <summary> /// Handle the Loaded event of the element to which this view model is attached /// in order 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; CircularProgressBarViewModel attachedModel = GetAttach(targetElement); // find the ProgressBar and associated it with the view model var progressBar = targetElement.Ancestors <ProgressBar>().Single() as ProgressBar; attachedModel.SetProgressBar(progressBar); }
/// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException("context"); } // collect all ancestor frames var frames = context.Ancestors().OfType <ModernFrame>().ToArray(); if (name == null || name == "_self") { // find first ancestor frame return(frames.FirstOrDefault()); } if (name == "_parent") { // find parent frame return(frames.Skip(1).FirstOrDefault()); } if (name == "_top") { // find top-most frame return(frames.LastOrDefault()); } // find ancestor frame having a name matching the target var frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame == null) { // find frame in scope of ancestor frame content var parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { var content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return(frame); }
public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException("context"); } var frames = context.Ancestors().OfType <ModernFrame>().ToArray(); if (name == null || name == "_self") { return(frames.FirstOrDefault()); } if (name == "_parent") { return(frames.Skip(1).FirstOrDefault()); } if (name == "_top") { return(frames.LastOrDefault()); } var frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { frame = context.FindName(name) as ModernFrame; if (frame == null) { var parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { var content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return(frame); }
private void OnDataGridPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (!(sender is VsDataGrid dataGrid)) { return; } FrameworkElement fe = GetFrameworkElement(e.OriginalSource); DataGridCell cell = fe?.Ancestors().OfType <DataGridCell>().FirstOrDefault(); if (cell == null || cell.IsEditing || cell.IsReadOnly || Keyboard.Modifiers != ModifierKeys.None) { return; } if (!cell.IsFocused) { cell.Focus(); } if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = cell.Ancestors().OfType <DataGridRow>().FirstOrDefault(); if (row == null || !row.IsSelected) { return; } row.IsSelected = true; } }
public static IEnumerable <FrameworkElement> VisualAncestors <T>(this FrameworkElement element) { return(element.Ancestors(toParent)); }