2011-06-23 42 views
1

我目前有一个将按钮内嵌图像对象的示例。 的XAML看起来是这样的:在按钮中嵌入图像对象

<Button Height="194" HorizontalAlignment="Left" 
    Margin="23,27,0,0" Name="button1" VerticalAlignment="Top" Width="216" 
    Click="button1_Click"> 
    <Image Name="image1" Stretch="Fill" 
     Source="/WPFButtonEmbedded;component/BenderInSpaceFace.png" 
     Height="105" VerticalAlignment="Bottom" Width="158" /> 
</Button> 

然而,当我尝试这个重建自己,我似乎得到一个错误。我虽然也许会把它们绑在一起,但看起来并不像那样。 此外,“image1”是我通过点击添加exise项目添加的图像。

任何意见或建议表示赞赏。

谢谢。

+0

你想设置图像的图像风格作为按钮背景? – Binil

+0

你得到了什么错误..?你有没有尝试将图像的构建操作属性设置为资源? – biju

+0

不只是将图像对象嵌入其中。图像对象可以像平常一样调整大小,但它在运行时实际嵌入。它很难用文字解释,但是当你运行它时,它看起来只是一个图片ontop,但它的按钮 – Johnston

回答

2

如果您需要设置图像内容 你可以通过设置样式的资源做

<Window.Resources> 
    <Image x:Key="Img" Source="/WPFButtonEmbedded;component/BenderInSpaceFace.png" Stretch="Fill"/> 
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="Content" Value="{StaticResource Img}" /> 
    </Style> 
</Window.Resources> 

,那么你可以设置按钮

<Button Style="{StaticResource ButtonStyle}" />