2015-07-28 116 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

否,因爲我們在後面的代碼中創建了一個堆棧面板來添加圖像或可能的控件,因此您需要以這種方式執行操作,因爲您需要以編程方式進行操作,但是您的方法與此完全不同。 –