<Button Canvas.Left="159" Canvas.Top="87" Height="35" Name="button2" Width="36" IsCancel="False" Click="button2_Click">
<Image Source="/WpfApplication1;component/bin/Debug/结束.png"></Image>
</Button>
//停止服务
private void button2_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("结束-灰色.png", UriKind.Relative);
BitmapImage bimg = new BitmapImage(uri);
button2.Background = new ImageBrush(bimg);
}
现在我在Click事件里面写没效果?
<Button Canvas.Left="159" Canvas.Top="87" Height="35" Name="button2" Width="36" IsCancel="False" Click="button2_Click">
<Image Source="/WpfApplication1;component/bin/Debug/结束.png"></Image>
</Button>
这个语法,其实呢,是把image作为了button按钮的conten属性了!你应该设置的是button的Background!如下:
<Grid>
<Button Canvas.Left="159" Canvas.Top="87" Name="button2" IsCancel="False" Click="button2_Click" Margin="0,67,-33,22">
</Button>
</Grid>
可以在前台设计button按钮的背景图片,也可以在后台加载:如下
public MainWindow()
{
InitializeComponent();
Uri uri = new Uri("图片路径", UriKind.Relative);
BitmapImage bimg = new BitmapImage(uri);
button2.Background = new ImageBrush(bimg);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("图片路径", UriKind.Relative);
BitmapImage bimg = new BitmapImage(uri);
button2.Background = new ImageBrush(bimg);
}
但是,这样虽然你的问题解决了,但是当鼠标移上去之后button的背景图片就为空了!解决这个问题的办法,是在鼠标移上事件里
编写事件。就ok了!
可以写个EventTrigger
private void button2_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("图片路径", UriKind.Relative);
BitmapImage bimg = new BitmapImage(uri);
button2.Background = new ImageBrush(bimg);
}
在Windows Phone8 里面不能这样写啊!
它说button2.Background = new ImageBrush(bimg);有错