2015-07-28 95 views
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(); 
     }  
    } 
} 

回答

1

我已经这样做了: 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; 
     } 
    } 
} 
+0

图片出现如果我写你如何,但如果我写:

+0

否,因为我们在后面的代码中创建了一个堆栈面板来添加图像或可能的控件,因此您需要以这种方式执行操作,因为您需要以编程方式进行操作,但是您的方法与此完全不同。 –