WPF显示SQLITE数据(三)

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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目录中

binDebugnet5.0-windowsResourcesImagetest.png

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

 

下面是依据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() + ":rn" +
43                 " Sender: " + sender.ToString() + "rn" +
44                 " Source: " + e.Source + "rn" +
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 }

 

脚本宝典总结

以上是脚本宝典为你收集整理的WPF显示SQLITE数据(三)全部内容,希望文章能够帮你解决WPF显示SQLITE数据(三)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。