private static void OnMarginChanged(object sender, DependencyPropertyChangedEventArgs e) { // Make sure this is put on a panel var panel = sender as Panel; if (panel == null) { return; } panel.Loaded += (s, ea) => { var p = sender as Panel; // Go over the children and set margin for them: foreach (var child in p.Children) { var fe = child as FrameworkElement; if (fe == null) { continue; } fe.Margin = MarginSetterExtension.GetMargin(p); } }; }
private static void ChangeMarginSide(FrameworkElement frameworkElement, MarginSides marginSide) { if (frameworkElement == null) { return; } var margin = frameworkElement.Margin; switch (marginSide) { case MarginSides.Left: margin.Left = MarginSetterExtension.GetMarginLeft(frameworkElement); break; case MarginSides.Top: margin.Top = MarginSetterExtension.GetMarginTop(frameworkElement); break; case MarginSides.Right: margin.Right = MarginSetterExtension.GetMarginRight(frameworkElement); break; case MarginSides.Bottom: margin.Bottom = MarginSetterExtension.GetMarginBottom(frameworkElement); break; } frameworkElement.Margin = margin; }
private static void OnMarginTopChanged(object sender, DependencyPropertyChangedEventArgs e) { MarginSetterExtension.ChangeMarginSide(sender as FrameworkElement, MarginSides.Top); }