WPF。使用c#将图像和文本添加到按钮中
WPF. Add image and text into button using c#
我有一个大按钮(灰色边框),我必须在其中使用 C# 添加图像,而且我还必须使用 C# 在图像下方添加文本(按钮内也有红色按钮),我真的这样做了不明白如何在 C# 代码(和文本)中添加图像以及如何调用它。应该是button.content=image之类的吧。。。找了一些资料,但是不明白用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>
我的客服:
namespace _3_Buttons
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
我这样做了:
xaml 文件:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button1" HorizontalAlignment="Left" Margin="127,69,0,0" VerticalAlignment="Top" Width="215" Height="110"/>
</Grid>
</Window>
xaml.cs 文件
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Image img = new Image();
string path = System.AppDomain.CurrentDomain.BaseDirectory + "chart2 - Copy.png";
img.Source = new BitmapImage(new Uri(path));
StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(10);
stackPnl.Children.Add(img);
button1.Content = stackPnl;
}
}
}
我有一个大按钮(灰色边框),我必须在其中使用 C# 添加图像,而且我还必须使用 C# 在图像下方添加文本(按钮内也有红色按钮),我真的这样做了不明白如何在 C# 代码(和文本)中添加图像以及如何调用它。应该是button.content=image之类的吧。。。找了一些资料,但是不明白用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>
我的客服:
namespace _3_Buttons
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
我这样做了: xaml 文件:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button1" HorizontalAlignment="Left" Margin="127,69,0,0" VerticalAlignment="Top" Width="215" Height="110"/>
</Grid>
</Window>
xaml.cs 文件
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Image img = new Image();
string path = System.AppDomain.CurrentDomain.BaseDirectory + "chart2 - Copy.png";
img.Source = new BitmapImage(new Uri(path));
StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(10);
stackPnl.Children.Add(img);
button1.Content = stackPnl;
}
}
}