/// <summary> /// Adapts the margin for the textblock. /// </summary> public static void AdaptMarginTextblock(BaseArcButton button) { var grid = button.Template.FindName("Grid", button) as Grid; var textblock = button.Template.FindName("Textblock", button) as TextBlock; var widthButton = MathUtil.Round((button.ActualWidth - MathUtil.Round(button.ActualWidth / button.Proportion)) / MathUtil.MarginCalculator(button.Proportion)); var heightButton = MathUtil.Round((button.ActualHeight - MathUtil.Round(button.ActualHeight / button.Proportion)) / MathUtil.MarginCalculator(button.Proportion)); Thickness margin = new Thickness(); switch (button.Tag) { case Position.Left: textblock.Width = widthButton; margin = new Thickness { Right = MathUtil.Round(grid.ActualWidth - widthButton) }; break; case Position.Top: margin = new Thickness { Bottom = MathUtil.Round(grid.ActualHeight - heightButton) }; break; case Position.Right: textblock.Width = widthButton; margin = new Thickness { Left = MathUtil.Round(grid.ActualWidth - widthButton) }; break; case Position.Bottom: margin = new Thickness { Top = MathUtil.Round(grid.ActualHeight - heightButton) }; break; } ; button.TextMargin = margin; }
/// <summary> /// Checks if there is Stroke on center button, and returns the offset to apply. /// </summary> public static double StrokeCenterChecker(BaseArcButton button) { return(button.StrokeThickness == 0 ? 0 : Round(button.StrokeThickness / 2)); }
public static BaseArcButton CreateBaseArcButton(double size, Position position, string text, double proportion = default(double), double strokeThickness = default(double)) { ArcButton = new BaseArcButton(text, position); if (proportion != default(double)) { ArcButton.Proportion = proportion; } if (strokeThickness != default(double)) { ArcButton.StrokeThickness = strokeThickness; } var template = new ControlTemplate(typeof(Button)); #region Triggers var mouseOverTrigger = new Trigger { Property = UIElement.IsMouseOverProperty, Value = true }; mouseOverTrigger.Setters.Add(new Setter { TargetName = "Path", Property = Shape.StrokeProperty, Value = new Binding { Path = new PropertyPath("BorderOver"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) } }); mouseOverTrigger.Setters.Add(new Setter { TargetName = "Path", Property = Shape.FillProperty, Value = new Binding { Path = new PropertyPath("BackgroundOver"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) } }); var pressedTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true }; pressedTrigger.Setters.Add(new Setter { TargetName = "Path", Property = Shape.StrokeProperty, Value = new Binding { Path = new PropertyPath("BorderBrushPressed"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) } }); pressedTrigger.Setters.Add(new Setter { TargetName = "Path", Property = Shape.FillProperty, Value = new Binding { Path = new PropertyPath("BackgroundPressed"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) } }); template.Triggers.Add(mouseOverTrigger); template.Triggers.Add(pressedTrigger); #endregion //Triggers #region Bindings var grid = new FrameworkElementFactory(typeof(Grid), "Grid"); grid.SetValue(FrameworkElement.HeightProperty, double.NaN); grid.SetValue(FrameworkElement.WidthProperty, double.NaN); grid.SetValue(UIElement.VisibilityProperty, new Binding { Path = new PropertyPath("Visibility"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1), Converter = new BoolToVisibilityConverter() }); var path = new FrameworkElementFactory(typeof(Path), "Path"); path.SetValue(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath("StrokeThickness"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); path.SetValue(Shape.FillProperty, new Binding { Path = new PropertyPath("Background"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); path.SetValue(Shape.StrokeProperty, new Binding { Path = new PropertyPath("BorderBrush"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); var textBlock = new FrameworkElementFactory(typeof(TextBlock), "Textblock"); textBlock.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); textBlock.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center); textBlock.SetValue(UIElement.VisibilityProperty, new Binding { Path = new PropertyPath("TextVisibility"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1), Converter = new BoolToVisibilityConverter() }); textBlock.SetValue(FrameworkElement.MarginProperty, new Binding { Path = new PropertyPath("TextMargin"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); textBlock.SetValue(TextBlock.FontFamilyProperty, new Binding { Path = new PropertyPath("FontFamily"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); textBlock.SetValue(TextBlock.ForegroundProperty, new Binding { Path = new PropertyPath("Foreground"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); textBlock.SetValue(TextBlock.FontSizeProperty, new Binding { Path = new PropertyPath("FontSize"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); textBlock.SetValue(TextBlock.TextProperty, new Binding { Path = new PropertyPath("Text"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Button), 1) }); #endregion //Bindings #region Position Path generatedPath = null; switch (position) { case Position.Left: grid.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Left); grid.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); textBlock.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Left); generatedPath = CreateSideButtonPath(size); path.SetValue(FrameworkElement.LayoutTransformProperty, new RotateTransform(180)); path.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Left); path.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); ArcButton.Name = "Left"; break; case Position.Top: grid.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); grid.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top); generatedPath = CreateSideButtonPath(size); path.SetValue(FrameworkElement.LayoutTransformProperty, new RotateTransform(270)); path.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); path.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top); ArcButton.Name = "Top"; break; case Position.Right: grid.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Right); grid.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); textBlock.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Left); generatedPath = CreateSideButtonPath(size); path.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Right); path.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); ArcButton.Name = "Right"; break; case Position.Bottom: grid.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); grid.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Bottom); generatedPath = CreateSideButtonPath(size); path.SetValue(FrameworkElement.LayoutTransformProperty, new RotateTransform(90)); path.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); path.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Bottom); ArcButton.Name = "Bottom"; break; case Position.Center: grid.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); grid.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); generatedPath = CreateCenterButtonPath(size); path.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); path.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); ArcButton.Name = "Center"; break; } path.SetValue(Path.DataProperty, generatedPath.Data); #endregion //Position grid.AppendChild(path); grid.AppendChild(textBlock); template.VisualTree = grid; ArcButton.Template = template; template.Seal(); return(ArcButton); }
/// <summary> /// Checks if there is Stroke on button, and returns the offset to apply. /// </summary> public static double StrokeCornerChecker(BaseArcButton button) { return(button.StrokeThickness == 0 ? 0 : Round(Math.Sqrt(2 * Math.Pow(button.StrokeThickness, 2)) / 2)); }