Example #1
0
        public static Style ButtonStyleWithTintColor(Color tintColor, Boolean shouldHaveDropdownIndicator)
        {
            new ImageDependencyProperty();

            Style buttonStyle = new Style();
            buttonStyle.TargetType = typeof(Button);
            buttonStyle.Setters.Add(new Setter(Control.OverridesDefaultStyleProperty, true));

            Setter templateSetter = new Setter();
            templateSetter.Property = Button.TemplateProperty;

            ControlTemplate template = new ControlTemplate(typeof(Button));

            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Border));
            factory.Name = "Border";
            factory.SetValue(Control.NameProperty, "Border");
            factory.SetValue(Border.CornerRadiusProperty, new CornerRadius(2.0));
            factory.SetValue(Border.BorderThicknessProperty, new Thickness(1.0));
            factory.SetValue(Border.BorderBrushProperty, new SolidColorBrush(tintColor.LighterColorBy(-0.5)));
            factory.SetValue(Border.BackgroundProperty, new LinearGradientBrush(tintColor.LighterColorBy(-0.00), tintColor.LighterColorBy(-0.15), 90));
            factory.SetValue(Control.UseLayoutRoundingProperty, true);

            FrameworkElementFactory childFactory = new FrameworkElementFactory(typeof(DockPanel));
            childFactory.SetValue(ContentPresenter.MarginProperty, new Thickness(kMinimumPadding / 2, 2.0, kMinimumPadding / 2, 2.0));
            //childFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

            FrameworkElementFactory imageViewFactory = new FrameworkElementFactory(typeof(Image));
            imageViewFactory.Name = "Image";
            imageViewFactory.SetValue(Control.NameProperty, "Image");
            imageViewFactory.SetValue(Control.SnapsToDevicePixelsProperty, true);
            imageViewFactory.SetValue(Image.MarginProperty, new Thickness(0.0, 0.0, kMinimumPadding / 4, 0.0));
            imageViewFactory.SetValue(Image.StretchProperty, Stretch.None);
            imageViewFactory.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            imageViewFactory.SetValue(Image.VerticalAlignmentProperty, VerticalAlignment.Center);
            imageViewFactory.SetValue(DockPanel.DockProperty, Dock.Left);

            Binding imageBinding = new Binding();
            imageBinding.Path = new PropertyPath(ImageDependencyProperty.ImageProperty);
            imageBinding.RelativeSource = RelativeSource.TemplatedParent;
            imageViewFactory.SetBinding(Image.SourceProperty, imageBinding);

            FrameworkElementFactory contentViewFactory = new FrameworkElementFactory(typeof(ContentPresenter));
            contentViewFactory.Name = "Content";
            contentViewFactory.SetValue(Control.NameProperty, "Content");
            contentViewFactory.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
            contentViewFactory.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            contentViewFactory.SetValue(ContentPresenter.EffectProperty, GradientBarTextEffect());

            if (!shouldHaveDropdownIndicator) {
                contentViewFactory.SetValue(ContentPresenter.MarginProperty, new Thickness(0, 0, kMinimumPadding / 2, 0));
            }

            FrameworkElementFactory menuIndicatorFactory = new FrameworkElementFactory(typeof(Image));
            menuIndicatorFactory.SetValue(Image.StretchProperty, Stretch.None);
            menuIndicatorFactory.SetValue(Image.SourceProperty, DropDownTriangle());
            menuIndicatorFactory.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Right);
            menuIndicatorFactory.SetValue(Image.MarginProperty, new Thickness(0.0, 0.0, kMinimumPadding / 4, 0.0));
            menuIndicatorFactory.SetValue(DockPanel.DockProperty, Dock.Right);

            childFactory.AppendChild(imageViewFactory);
            childFactory.AppendChild(contentViewFactory);

            if (shouldHaveDropdownIndicator) {
                childFactory.AppendChild(menuIndicatorFactory);
            }

            factory.AppendChild(childFactory);

            template.VisualTree = factory;

            // Triggers

            Trigger onMouseOver = new Trigger();
            onMouseOver.Property = Button.IsMouseOverProperty;
            onMouseOver.Value = true;
            onMouseOver.Setters.Add(new Setter(Button.BackgroundProperty,
                new LinearGradientBrush(tintColor.LighterColorBy(0.025), tintColor.LighterColorBy(-0.125), 90),
                "Border"));

            Trigger onMouseDown = new Trigger();
            onMouseDown.Property = Button.IsPressedProperty;
            onMouseDown.Value = true;
            onMouseDown.Setters.Add(new Setter(Button.BackgroundProperty,
                new LinearGradientBrush(tintColor.LighterColorBy(-0.15), tintColor.LighterColorBy(-0.30), -90),
                "Border"));
            onMouseDown.Setters.Add(new Setter(ContentPresenter.EffectProperty, null, "Content"));

            template.Triggers.Add(onMouseOver);
            template.Triggers.Add(onMouseDown);

            templateSetter.Value = template;

            buttonStyle.Setters.Add(templateSetter);

            return buttonStyle;
        }