我正在尝试将任何Mahapps.Metro.IconPacks中的图标分别分配给一个带有文本的按钮。
如果我想使用新的IconPacks,我该怎么做?
实际上我需要将Mahapps.Metro.IconPacks.PackIconModernKind转换为VisualBrush或canvas,或者实际上我可以用来将它放在按钮中。
感谢任何帮助!
由于
答案 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>
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;
希望这有帮助。