向按钮添加 material 个设计图标

Add material design icon to button

我试图找到一种方法来以编程方式将 material 设计块中的图标显示到我的按钮,我取得了一些进展,但我认为最重要的问题是我没有这条线 "Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" 来自 XAML 在我的 C# 代码中,我不知道如何实现它。

这是我在 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>

看起来像这样:

这是我用 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;

看起来像这样:

我也这样做了,但似乎不起作用: Style = Resources["MaterialDesignFloatingActionMiniAccentButton"] as Style

解决方案是从 Resources 字典中为按钮设置“默认样式”。该字典应包含应用程序的所有样式。您可以将其分配为:

deleteRow.Style = Resources["MaterialDesignFloatingActionMiniAccentButton"] as Style;