1
我有大按钮(灰色边框),我必须使用c#添加图像,还必须使用c#在图像下添加文本(按钮内部还有红色按钮),我真的不明白如何在c#代码(和文本)中添加图像以及如何调用它。它必须是像button.content =图片...我发现了一些信息,但我不明白在c#中写什么?WPF。使用c#将图像和文字添加到按钮中#
我的XAML:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="20px" />
<Setter Property="ToolTipService.IsEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="#E6E6E6" BorderThickness="1">
<Grid x:Name="btnGrid" Width="320px" Height="320px">
<Image Grid.Row="0"
Stretch="Fill"
HorizontalAlignment="Center"
Width="131"
Height="98"
Margin="0,69px,0,0"
Source="{TemplateBinding Content}" />
<TextBlock x:Name="btnText" Grid.Row="0"
Text="{TemplateBinding ToolTip}"
HorizontalAlignment="Center"
Margin="0,207,0,0"
TextAlignment="Center"
TextWrapping="Wrap"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
FontStretch="{TemplateBinding FontStretch}" />
<Button x:Name="btnButton" Grid.Row="2" Style="{DynamicResource MetroButtonStyle}"
ToolTip="{TemplateBinding ToolTip}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="#F86C4D"
Margin="0,238px,0,0"
Height="50"
Width="150"
Foreground="White"
FontFamily="{TemplateBinding FontFamily}"
/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的CS:
namespace _3_Buttons
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
图片出现如果我写你如何,但如果我写:图像消失,它可能添加图像和我写的使用风格? (样式代码是在xaml代码中) – LTU
否,因为我们在后面的代码中创建了一个堆栈面板来添加图像或可能的控件,因此您需要以这种方式执行操作,因为您需要以编程方式进行操作,但是您的方法与此完全不同。 –