2016-01-13 27 views
0

我已经创建了一个WPF应用程序。我需要在按钮上显示图像。所以我应该在哪里存储该图像在我的应用程序以及如何设置图像的源属性? 请重播带图像的WPF应用程序按钮

+2

欢迎来到StackOverflow。你试过什么了?检查“[如何问](http://stackoverflow.com/help/how-to-ask)”并编辑您的问题。 – aloisdg

回答

0
<Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400"> 
     <Button.Background> 
      <ImageBrush **ImageSource ="Images/AERO.png"** ></ImageBrush> 
     </Button.Background> 
    </Button> 

在CS文件:(店面形象在资源或项目目录的任何文件夹)

private void Button1_Click_1(object sender, RoutedEventArgs e) 
{ 
    var brush = new ImageBrush(); 
    brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png")); 
    Button1.Background = brush; 
} 
0
  1. 一个图像添加到您的项目。
  2. 设置此图片的财产:'不得复制'和'资源'
  3. 添加一个按钮,删除它的默认'内容'属性。
  4. 添加图像控件作为此按钮的内容。

<Grid> 
     <Button x:Name="button" HorizontalAlignment="Left" Margin="190,136,0,0" VerticalAlignment="Top" Width="104" Height="42"> 
      <Image Source="pen.png" /> 
     </Button> 
    </Grid> 
+1

你真的分享代码作为截图吗? – aloisdg

0

您可以在一个按钮添加喜欢的图像,

<Button> 
    <StackPanel> 
     <Image Source="Images/imageName.png" /> 
    </StackPanel> 
</Button> 

要不然你可以试试,

<Window.Resources> 
     <ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" /> 
</Window.Resources> 
    <Grid> 
     <Button Background="{StaticResource MyResource}"/> 
    </Grid> 

否则试试这个。

<Window.Resources> 
    <ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" /> 
</Window.Resources> 
<Grid> 
    <Button> 
     <Button.Content> 
      <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="34" Height="19" Margin="0"> 
       <TextBlock.Background> 
        <StaticResource ResourceKey="MyResource"/> 
       </TextBlock.Background> 
      </TextBlock> 
     </Button.Content> 
    </Button> 
</Grid> 
0

添加图片到项目和设置图像属性:

  1. 生成操作:资源
  2. 复制到输出目录:不要在的.xaml文件复制

然后添加一个按钮(例如):

<Button HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="40" Height="40"> 
    <Image Source="Resources/yourImageName.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
</Button>