[英]Assign Mahapps.Metro:IconPacks to button in code behind
I am trying to assign an icon from any Mahapps.Metro.IconPacks to an rectangle respectively to a button with text. 我正在尝试将任何Mahapps.Metro.IconPacks中的图标分别分配给带有文本的按钮的矩形。
How do I do this if I want to use the new IconPacks? 如果要使用新的IconPack,该怎么办?
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. 实际上,我需要将Mahapps.Metro.IconPacks.PackIconModernKind转换为VisualBrush或canvas或实际上可以用来将其放置在按钮中的任何东西。
Any help is appreciated! 任何帮助表示赞赏!
Thanks 谢谢
You can place any IconPack Control directly to the content of the Button or to a container like a Grid
. 您可以将任何IconPack控件直接放置在Button的内容中,也可以放置在诸如
Grid
类的容器中。
The Xaml way 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>
or 要么
<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>
The code behind way of Xaml version 2 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;
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.