Search code examples
c#wpfmodern-ui

How to work with with PathGeometry Class


I'm using ModernUI and there is a thing called "Modern Buttons" that are basically buttons but with different skin. The particular characteristic is the way the image in the button is drawn: it uses PathGeometry. This geometry can be found at this site. What I need is to change that PathGeometry programmatically code-behind. I know how to do it in XAML, for example:

 IconData="F1 M 19.0002,34L 19.0002,42L 43.7502,42L 33.7502,52L 44.2502,52L 58.2502,38L 44.2502,24L 33.7502,24L 43.7502,34L 19.0002,34 Z " />

That staff represents an arrow.

But I can't do the same code-behind, I think I need some kind of transformation. In the source code of ModernUI I've found this piece of code where a bunch of buttons referenced are placed. This is also a good option for me.

 public ControlsModernButton()
    {
        InitializeComponent();

        // find all embedded XAML icon files
        var assembly = GetType().Assembly;
        var iconResourceNames = from name in assembly.GetManifestResourceNames()
                                where name.StartsWith("FirstFloor.ModernUI.App.Assets.appbar.")
                                select name;


        foreach (var name in iconResourceNames) 
        {
            // load the resource stream
            using (var stream = assembly.GetManifestResourceStream(name))
            {
                // parse the icon data using xml
                var doc = XDocument.Load(stream);

                var path = doc.Root.Element("{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Path");
                if (path != null) {
                    var data = (string)path.Attribute("Data");

                    // create a modern button and add it to the button panel
                    ButtonPanel.Children.Add(new ModernButton {
                        IconData = PathGeometry.Parse(data),
                        Margin = new Thickness(0, 0, 4, 8)
                    });
                }
            }
        }
    }

The difference with my case is that I don't want to create a new button but change it IconData property.

That files (the ones that contain the geometry) have this structure inside:

    <?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_add" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="38" Height="38" Canvas.Left="19" Canvas.Top="19" Stretch="Fill" Fill="#FF000000" Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z "/>
</Canvas>

I think the best option is to be able to set that IconData property from a .png file but I don't know the way of doing it in code.


Solution

  • I found it! In my case the code is:

                      var streamGeometry = StreamGeometry.Parse("F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z ");
                        sortButton.IconData = streamGeometry;
    

    Maybe this is useful to somebody.