Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PathIcon from code behind?

How would I write the following XAML in code behind? I'm having a problem understanding the PathIcon Data part of it.

<AppBarToggleButton Label="PathIcon" Click="AppBarButton_Click">
    <AppBarToggleButton.Icon>
        <PathIcon Data="F1 M 20,20L 24,10L 24,24L 5,24"/>
    </AppBarToggleButton.Icon>
</AppBarToggleButton>
like image 310
Brian Avatar asked Feb 03 '26 05:02

Brian


1 Answers

Try,

this.appBarButton.Icon = new PathIcon(){ Data = PathMarkupToGeometry(Your xaml data string)};

in this case: xaml data string is F1 M 20,20L 24,10L 24,24L 5,24

Geometry PathMarkupToGeometry(string pathMarkup)
{
        string xaml =
        "<Path " +
        "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
        "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
        var path = XamlReader.Load(xaml) as Windows.UI.Xaml.Shapes.Path;
        // Detach the PathGeometry from the Path
        Geometry geometry = path.Data;
        path.Data = null;
        return geometry;
}

Hope this helps you :)

like image 166
Prakash Selvaraj Avatar answered Feb 05 '26 17:02

Prakash Selvaraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!