将Mahapps.Metro:IconPacks分配给后面代码中的按钮 [英] Assign Mahapps.Metro:IconPacks to button in code behind

查看:164
本文介绍了将Mahapps.Metro:IconPacks分配给后面代码中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to assign an icon from any Mahapps.Metro.IconPacks to an rectangle respectively to a button with text.

如果我要怎么做要使用新的IconPacks?

How do I do this if I want to use the new IconPacks?

有效地,我需要将Mahapps.Metro.IconPacks.PackIconModernKind转换为VisualBrush或canvas或实际上可以用来将其放置在按钮中的任何东西。

Effectively I need to convert an Mahapps.Metro.IconPacks.PackIconModernKind to VisualBrush or canvas or actually anything I can use to place it in a button.

感谢您的帮助!

谢谢

推荐答案

您可以将任何IconPack控件直接放置在Button的内容中,也可以放置在 Grid 之类的容器中。

You can place any IconPack Control directly to the content of the Button or to a container like a 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>

Xaml版本2背后的代码

The code behind way of Xaml version 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;

希望这会有所帮助。

这篇关于将Mahapps.Metro:IconPacks分配给后面代码中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆