Demo-WPF中Button按钮点击切换Image图片例子

WPF中点击按钮切换Image图片

——墨问苍生


 

学习WPF,写了一个例子,通过按钮切换Image显示的图片

前台代码:

<Window x:Class="Demo01.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Demo01" Height="400" Width="600">
    <Grid>
        <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
<Image Name="img_light1" Grid.Row="0" Grid.Column="2" Width="60" Height="60" Source="res/Light_Close.png"></Image> <Button Click="Button_Click" Grid.Row="0" Grid.Column="3" Height="40" Width="70" Content="开灯"></Button>

</Grid> </Window>

界面效果:

 

 后台代码:

/*
 * Created by SharpDevelop.
 * User: W2268
 * Date: 2020/10/26
 * Time: 23:31
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Demo01
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void Button_Click(object sender, RoutedEventArgs e)
        {
            //切换图片逻辑,相对路径
            img_light1.Source=new BitmapImage(new Uri(@"res/Light_Open.png",UriKind.RelativeOrAbsolute));
        }
    }
}

 

posted @ 2020-10-28 01:57  墨问苍生  阅读(2745)  评论(0编辑  收藏  举报