Setting icon of System.Windows.Controls.Button using SVG converted to XAML?

Darren Rose 311 Reputation points
2022-08-07T13:17:41.517+00:00

With a System.Windows.Controls.MenuItem I can set the icon as below:-

CloseMenuItem.Icon = CType(System.Windows.Markup.XamlReader.Parse("<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'  Data='F1M16,1.166L14.834,0 8,6.834 1.166,0 0,1.166 6.834,8 0,14.834 1.166,16 8,9.166 14.834,16 16,14.834 9.166,8z' Stretch='Uniform' HorizontalAlignment='Center' Fill='#55475F77' IsEnabled = 'False'/>"), System.Windows.Shapes.Path)  

How can I do the same with a System.Windows.Controls.Button which doesn't have an .Icon property but instead a .Content property which if i try and use above gives me "value of type 'path' cannot be converted to 'imagesource'." error message.

Currently I am just pointing to an PNG file, but would rather do it like above if possible

 MyButton.Content = New System.Windows.Controls.Image With {  
            .Source = New System.Windows.Media.Imaging.BitmapImage(New Uri(Path.Combine(My.Application.Info.DirectoryPath, "\Toolbar\MyIcon.png"), UriKind.RelativeOrAbsolute)),  
            .Width = 32,  
            .Height = 32,  
            .Margin = New System.Windows.Thickness(2, 0, 0, 0)  
        }  

Also what is best way of converting an SVG to the XAML code needed to then use in above?

Thanks

Developer technologies | VB
Developer technologies | XAML
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-08-07T14:49:57.307+00:00

    You can do for example :

        Dim path As Path = New Path()  
        path.Data = Geometry.Parse("F1M16,1.166L14.834,0 8,6.834 1.166,0 0,1.166 6.834,8 0,14.834 1.166,16 8,9.166 14.834,16 16,14.834 9.166,8z")  
        path.Stroke = Brushes.Red  
        path.StrokeThickness = 1  
        ' path.Fill = New SolidColorBrush(CType(ColorConverter.ConvertFromString("#55475F77"), Color))  
        MyButton.Content = path  
    

    I get :

    228830-button-path.jpg


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.