• WPF显示SQLITE数据(三)


    关于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目录中

    binDebug et5.0-windowsResourcesImage est.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() + ":
    " +
    43                 " Sender: " + sender.ToString() + "
    " +
    44                 " Source: " + e.Source + "
    " +
    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 }
  • 相关阅读:
    文件共享锁定数溢出(AC等修改数据多出错)
    智能五子棋基本思路
    API与C#数据结构类型对应关系表
    读书释疑C#浅拷贝与深拷贝
    冒泡排序(C#数据结构学习九)
    希尔排序(C#数据结构学习八)
    fatal error LNK1169: 找到一个或多个多重定义的符号
    Solo Spirit系统精灵(含C#源码)
    插入排序(C#数据结构学习七)
    快速排序(C#数据结构学习十)
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/15450940.html
Copyright © 2020-2023  润新知