1

I try to find a way to display an icon from the material design nugget to my buttons programmatically, I made some progress but I think the most important problem is that I don't have this line "Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" from XAML in my C# Code, and I don't know how to implement this.

This is my Button in XAML:

<Button
    x:Name="AddRowInHexConverter"
    Grid.Row="0"
    Grid.Column="4"
    Width="20"
    Height="20"
    Margin="0,0,0,0"
    HorizontalAlignment="Center"
    VerticalAlignment="Bottom"
    Background="#FF2196F3"
    BorderBrush="#FF2196F3"
    Click="AddRowInHexConverter_Click"
    Foreground="White"
    Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}">
    <materialDesign:PackIcon
        Width="20"
        Height="20"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Kind="Add" />
    <Button.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="5" />
        </Style>
    </Button.Resources>
</Button>

and look like this:

enter image description here

This is my button in C#:

Button deleteRow = new Button();
    
deleteRow.Width = 20;
deleteRow.Height = 20;
deleteRow.HorizontalAlignment = HorizontalAlignment.Center;
deleteRow.VerticalAlignment = VerticalAlignment.Bottom;
deleteRow.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#FF2196F3");
deleteRow.Foreground = new SolidColorBrush(Colors.White);
deleteRow.BorderBrush = (SolidColorBrush)new BrushConverter().ConvertFrom("#FF2196F3");
deleteRow.Click += DeleteRowFromGrid_Click;

PackIcon icon = new PackIcon();
icon.Kind = PackIconKind.Minus;
icon.Foreground = new SolidColorBrush(Colors.White);
icon.Height = 20;
icon.Width = 20;
icon.HorizontalAlignment = HorizontalAlignment.Center;
icon.VerticalAlignment = VerticalAlignment.Center;

deleteRow.Content = icon;

and look like this:

enter image description here

I also done this but seems to doesn't work: Style = Resources["MaterialDesignFloatingActionMiniAccentButton"] as Style

Vesa95
  • 607
  • 1
  • 7
  • 26
  • Does this answer your question? [How to set the style programmatically](https://stackoverflow.com/questions/3199424/how-to-set-the-style-programmatically) – Tatranskymedved Dec 11 '20 at 09:29
  • I does't work.. I done like this: Style = Resources["MaterialDesignFloatingActionMiniAccentButton"] as Style – Vesa95 Dec 11 '20 at 09:32

1 Answers1

1

Solution is to set "default style" for button from the Resources dictionary. This dictionary should containt all styles for the application. And you can assign it as:

deleteRow.Style = Resources["MaterialDesignFloatingActionMiniAccentButton"] as Style;
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47