孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

关于WPF的图片显示与事件冒泡

显示图片资源可以用

<Image Name="img_role" Loaded="img_role_Loaded" Stretch="None" MouseUp="SomethingClicked" />

并在代码中写入

1 private void img_role_Loaded(object sender, RoutedEventArgs e)
2 {
3     BitmapImage bi = new BitmapImage();
4     bi.BeginInit();
5     bi.UriSource = new Uri(@"Resources/Image/test.png", UriKind.RelativeOrAbsolute);
6     bi.EndInit();
7     img_role.Source = bi;
8 }

其中图片test.png位于生成的net5.0-windows目录中

bin\Debug\net5.0-windows\Resources\Image\test.png

或者直接选择Image图像的属性编辑器,在Source属性中选择图片,会自动拷贝到bin目录下,我们在<Image Source="/test.png"  ...  /> 即可

 

下面是依据WPF编程宝典的事件冒泡做的效果:

<Window x:Class="routeevent.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:routeevent"
        mc:Ignorable="d"
        Title="MainWindow" Height="497" Width="870" MouseUp="SomethingClicked">
    <Grid Margin="3" MouseUp="SomethingClicked">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Margin="5,5,0,5" Grid.Row="0" HorizontalAlignment="Left" Background="AliceBlue" 
               BorderBrush="Black" BorderThickness="1" MouseUp="SomethingClicked">
            <StackPanel MouseUp="SomethingClicked">
                <TextBlock Margin="3" MouseUp="SomethingClicked">Image and text label</TextBlock>
                <Image Stretch="UniformToFill" MouseUp="SomethingClicked" Source="/test.png" Height="21" Width="171" />
                <TextBlock Margin="3" MouseUp="SomethingClicked">Courtesy of the StackPanel"</TextBlock>
            </StackPanel>
        </Label>
        <ListBox Grid.Row="1" Margin="5,5,5,5" Name="lstMessages" Grid.ColumnSpan="2"></ListBox>
        <CheckBox Grid.Row="2" Margin="5,5,5,5" Name="chkHandle" Grid.ColumnSpan="2">
            Handle first event
        </CheckBox>
        <Button Grid.Row="2" Margin="787,21,0,5" Padding="3" HorizontalAlignment="Left" 
                    Name="cmdClear" Click="cmdClear_Click" Width="72" Grid.RowSpan="2">Clear List</Button>
    </Grid>
</Window>

后台MainWindow.xaml.cs为

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace routeevent
17 {
18     /// <summary>
19     /// Interaction logic for MainWindow.xaml
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27 
28         private void img_role_Loaded(object sender, RoutedEventArgs e)
29         {
30             //BitmapImage bi = new BitmapImage();
31             //bi.BeginInit();
32             //bi.UriSource = new Uri(@"Resources/Image/test.png", UriKind.RelativeOrAbsolute);
33             //bi.EndInit();
34             ////MessageBox.Show(bi.Width.ToString());
35             //img_role.Source = bi;
36         }
37 
38         protected int eventCounter = 0;
39         private void SomethingClicked(object sender, MouseButtonEventArgs e)
40         {
41             eventCounter++;
42             string message = "#" + eventCounter.ToString() + ":\r\n" +
43                 " Sender: " + sender.ToString() + "\r\n" +
44                 " Source: " + e.Source + "\r\n" +
45                 " Original Source: " + e.OriginalSource;
46             lstMessages.Items.Add(message);
47             e.Handled = (bool)chkHandle.IsChecked;
48         }
49 
50         private void cmdClear_Click(object sender, RoutedEventArgs e)
51         {
52             lstMessages.Items.Clear();
53         }
54     }
55 }

 

posted on 2021-10-24 14:03  孤独的猫  阅读(178)  评论(0编辑  收藏  举报