猿问
下载APP

请问下我在WPF里面怎设置点击butooin改变butooin的背景图片啊?

<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事件里面写没效果?


慕神8447489
浏览 229回答 2
2回答

Qyouu

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

撒科打诨

&nbsp; private void button2_Click(object sender, RoutedEventArgs e)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Uri uri = new Uri("图片路径", UriKind.Relative);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BitmapImage bimg = new BitmapImage(uri);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; button2.Background = new ImageBrush(bimg);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }在Windows Phone8 里面不能这样写啊!它说button2.Background = new ImageBrush(bimg);有错
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答