【问题标题】:Add material design icon to button将材料设计图标添加到按钮
【发布时间】:2020-12-11 17:41:32
【问题描述】:

我试图找到一种方法,以编程方式从材料设计块显示图标到我的按钮,我取得了一些进展,但我认为最重要的问题是我的 XAML 中没有这行 "Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" 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

【问题讨论】:

标签: c# xaml material-design


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2016-01-12
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2020-12-26
    • 2016-06-28
    相关资源
    最近更新 更多