question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change svg fill and brush colour via binding

See original GitHub issue

A useful feature would be to allow the color defined in the svg to be overridden from a xaml binding (or indeed code behind).

Some use cases that come to mind…

  • Support ‘hover over’ color changing effect, e.g. allow a binding to be created to change the color when during a mouse over event.
  • Support theming, e.g. allow a dark theme to override the default svg color
  • Support ‘off the shelf’ svg libraries, e.g. allow a quick way to change a monotone svg (typically black) to something more useful without having to edit the svg.

AFAIK, it’s been discussed elsewhere, e.g. #163, #88.

The following code snippet (heavily based on https://github.com/ElinamLLC/SharpVectors/issues/88#issuecomment-576503027) integrated into SvgViewbox would provide the functionality to override the ‘fill’ and ‘stroke’ brushes. It would need a bit of improvement though to remove the default color brushes mind you.

    public class SvgViewboxEx : SvgViewbox
    {
        public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Brush), typeof(SvgViewboxEx), new PropertyMetadata(Brushes.BlueViolet));

        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(SvgViewboxEx), new PropertyMetadata(Brushes.DarkViolet));

        public Brush Fill
        {
            get => (Brush) GetValue(FillProperty);
            set => SetValue(FillProperty, value);
        }

        public Brush Stroke
        {
            get => (Brush) GetValue(StrokeProperty);
            set => SetValue(StrokeProperty, value);
        }

        protected override void OnSettingsChanged()
        {
            base.OnSettingsChanged();

            var drawings = ((SvgDrawingCanvas) Child).DrawObjects;

            foreach (var drawing in drawings)
            {
                if (drawing is GeometryDrawing geometryDrawing)
                {
                    // svg fill color - translated to a geometry.Brush
                    var brush = new Binding(nameof(Fill))
                                    {
                                        Source = this,
                                        Mode = BindingMode.OneWay
                                    };
                    BindingOperations.SetBinding(geometryDrawing, GeometryDrawing.BrushProperty, brush);

                    // svg stroke color - translated to a geometry.Pen.Brush
                    if (geometryDrawing.Pen != null)
                    {
                        var stroke = new Binding(nameof(Stroke))
                                         {
                                             Source = this,
                                             Mode = BindingMode.OneWay
                                         };
                        BindingOperations.SetBinding(geometryDrawing.Pen, Pen.BrushProperty, stroke);
                    }
                }
            }
        }
    }

At any rate, the above code can be used verbatim for anyone wanting this feature without waiting for an equivalent in SharpVectors.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
r-tmpcommented, Sep 6, 2022

@paulushub Just tested it in a small project (.NET 6), not much to say except that seems to be working as expected 🙂.

0reactions
paulushubcommented, Oct 24, 2022

Sorry for the delay, release 1.8.1 is now available.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to swap SVG Fill Color on MouseOver? - wpf
Search for all such Brush in your code and change their values, thus creating two copies of your SVG XAML , then using...
Read more >
Change Color of SVG on Hover
Let's say you're using inline SVG and want to change the SVG icon that is displayed in an element on state change, like...
Read more >
How can I change a svg image's color
Hi, I want to use a svg image as a SimpleButton's Glyph, and set it's foreground color blue. when I move mouse over...
Read more >
WPF SVG Images - Use Palettes to Replace Image Colors
This example demonstrates how to change SVG image colors for a theme ... Key property specifies the color replaced with the SolidColorBrush.
Read more >
how to create SVGs and changing the fill color dynamically ...
SVGs allow changing of fill colors, etc., dynamically. Create your SVG using your favourite program. In this example I've used Inkscape.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found