将Mahapps.Metro:IconPacks分配给代码后面的按钮

时间:2016-12-13 09:02:38

标签: c# code-behind mahapps.metro

我正在尝试将任何Mahapps.Metro.IconPacks中的图标分别分配给一个带有文本的按钮。

如果我想使用新的IconPacks,我该怎么做?

实际上我需要将Mahapps.Metro.IconPacks.PackIconModernKind转换为VisualBrush或canvas,或者实际上我可以用来将它放在按钮中。

感谢任何帮助!

由于

1 个答案:

答案 0 :(得分:6)

您可以将任何IconPack Control直接放置到Button的内容或像Grid这样的容器中。

Xaml方式

<Button Content="Test">
    <Button.ContentTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <iconPacks:PackIconMaterial Kind="Cookie"
                                            Margin="4 4 2 4"
                                            Width="24"
                                            Height="24"
                                            VerticalAlignment="Center" />
                <TextBlock Text="{Binding}"
                            Margin="2 4 4 4"
                            VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

<Button>
    <StackPanel Orientation="Horizontal">
        <iconPacks:PackIconMaterial Kind="Cookie"
                                    Margin="4 4 2 4"
                                    Width="24"
                                    Height="24"
                                    VerticalAlignment="Center" />
        <TextBlock Text="Test"
                    Margin="2 4 4 4"
                    VerticalAlignment="Center" />
    </StackPanel>
</Button>

enter image description here

Xaml版本2背后的代码

var stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
var packIconMaterial = new PackIconMaterial()
{
    Kind = PackIconMaterialKind.Cookie,
    Margin = new Thickness(4, 4, 2, 4),
    Width = 24,
    Height = 24,
    VerticalAlignment = VerticalAlignment.Center
};
stackPanel.Children.Add(packIconMaterial);
var textBlock = new TextBlock()
{
    Text = "Test",
    Margin = new Thickness(2, 4, 4, 4),
    VerticalAlignment = VerticalAlignment.Center
};
stackPanel.Children.Add(textBlock);
this.TestButton.Content = stackPanel;

希望这有帮助。