WPF浮窗Popup控件与命令Command简单使用

这篇具有很好参考价值的文章主要介绍了WPF浮窗Popup控件与命令Command简单使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

<Window x:Class="WPFLearn.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:WPFLearn"
        mc:Ignorable="d"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors" //这是引用包的地址
        d:DataContext="{d:DesignInstance Type=local:MainWindowVM}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Label Foreground="Blue" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" x:Name="ShowPicture" >
           //要引用微软官方包CommonServiceLocator
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    <i:InvokeCommandAction Command="{Binding ImageShowCMD}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
           
            <Label.Content  >查看示例</Label.Content>
        </Label>
        <Popup x:Name="Pop" PopupAnimation="Slide" IsOpen="{Binding IsPopShow}" PlacementTarget="{Binding ElementName=ShowPicture}"
               Placement="MousePoint" AllowsTransparency="True" StaysOpen="False">
            <Grid>
                <Image Source="/WPFLearn;component/Resources/down@2x.png"></Image>
            </Grid> 
            
        </Popup>
    </Grid>
</Window>

上面是WPF前端代码,其中图片Image要自己在项目下新建一个Resource文件夹把图片添加进去,WPF后台窗体代码为:

using System.Windows;

namespace WPFLearn
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowVM();//赋值上下文给MainWindowVM这个类
        }
    }
}

在另外新建一个类,俗称MVVM模式中的VM文件,文件暂时命名为MainWindowVM,类文件内容如下:

using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WPFLearn
{

    [Serializable]
    public class PropertyChangedObj : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string _property)
        {
            PropertyChangedEventHandler eventhandler = this.PropertyChanged;
            if (null == eventhandler)
                return;
            eventhandler(this, new PropertyChangedEventArgs(_property));
        }
    }


    public class MainWindowVM : PropertyChangedObj
    {
        private bool _isPopShow = false;
        public bool IsPopShow
        {
            get => _isPopShow;
            set
            {
                _isPopShow = value;
                OnPropertyChanged(nameof(IsPopShow));
            }
        }
        public ICommand ImageShowCMD => new RelayCommand(() =>
        {
            IsPopShow = true;
        });

    }
}

值得注意的是要引用包CommonServiceLocator,操作如下:右击项目名-属性-NuGet管理包,检索包的名称,不然就会保持文章来源地址https://www.toymoban.com/news/detail-732432.html

到了这里,关于WPF浮窗Popup控件与命令Command简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • WPF-UI HandyControl 控件简单实战+IconPacks矢量图导入

    因为HandyControl 的功能非常的丰富,我打算完整的了解一下HandyControl 整个控件的基本使用,而且我的网易云WPF项目也打算用UserControl 进行重构 WPF-UI HandyControl 简单介绍 HandyControl Visual Studio 插件 HandyControl Github地址 HandyControl 官方中文文档 HandyControl 实战Gitee仓库 我们下载了Han

    2024年02月02日
    浏览(44)
  • 浅谈WPF之Popup弹出层

    在日常开发中,当点击某控件时,经常看到一些弹出框,停靠在某些页面元素的附近,但这些又不是真正的窗口,而是页面的一部分,那这种功能是如何实现的呢?今天就以一个简单的小例子,简述如何 在WPF开发中,通过Popup实现鼠标点击弹出浮动停靠窗口 ,仅供学习分享使

    2024年02月03日
    浏览(41)
  • WPF图形控件使用之-Line线控件使用

    在项目中有的时候可能会用的画虚线或者设置线的流动效果,这个时候可能会使用到线控件。 属性 说明 描述 X1 起始x轴坐标 X1=\\\"10\\\" Y1 起始Y轴坐标 Y1=\\\"10\\\" X2 结束X轴坐标 X2=\\\"100\\\" Y2 结束Y轴坐标 Y2=\\\"100\\\" Stroke 线条颜色 Stroke=\\\"Red\\\" StrokeThickness 线条粗细 StrokeThickness=\\\"2\\\" StrokeDashArray 设置

    2024年02月13日
    浏览(30)
  • 【WPF应用39】WPF 控件深入解析:SaveFileDialog 的属性与使用方法

    在 Windows Presentation Foundation (WPF) 中,SaveFileDialog 控件是一个非常重要的文件对话框,它允许用户在文件系统中选择一个位置以保存文件。这个控件提供了很多属性,可以自定义文件对话框的显示内容和行为。 本文将详细介绍 SaveFileDialog 控件的属性和功能,如何在 WPF 应用程序

    2024年04月12日
    浏览(35)
  • WPF编程--地图控件GMap使用

    目录 ​编辑 1.环境 2. NuGet导入依赖 3.  添加MapControl类 4. 编辑MainView.xaml.cs 5. 编辑MainView.xaml 6. 启动验证 源码 : https://github.com/liugang198409/WpfDemo/tree/master/GMapDemo  视频 :WPF编程--地图控件GMap_哔哩哔哩_bilibili VVisual Studio 2019 + .NET Framework 4.8.1 导入依赖GMap.NET.Presentation

    2024年02月10日
    浏览(30)
  • WPF 实现Popup不在最上层显示、随窗口移动

    由于WPF 默认的Popup总是显示在所有窗口的前面,如何让popup 层只显示在该父级之上,并随着父级而动呢?下面来看实现。 代码如下(示例):

    2024年01月20日
    浏览(42)
  • WPF中使用WebView2控件

    WebView2 全称 Microsoft Edge WebView2 控件,此控件的作用是在本机桌面应用中嵌入web技术(html,css,javascript),从名字就可以看出来WebView2使用了Edge内核渲染web内容。 通俗来说,WebView2控件是一个UI组件,允许在桌面应用中提供web能力的集成,即俗称的混合开发。 助力程序开发和

    2024年02月03日
    浏览(38)
  • 如何使用 WPF 用户控件关闭父窗口

    How to close parent windows using WPF User Control 如何使用 WPF 用户控件关闭父窗口 【问题描述】 假设有两个WPF窗口:window1和window2。 window1有一个按钮,单击此按钮将打开window2。window2包含一个用户控件。此用户控件有一个用于关闭window2的按钮。 怎样才能实现这个场景呢? 【解决方案

    2024年02月15日
    浏览(35)
  • 如何在WPF中使用Winform控件

            要在WPF中使用WInform组件,必须将WInform组件放在宿主WindowsFormsHost中.  WindowsFormsHost 是WPF的一个控件,它允许在WPF应用程序中托管Windows Forms控件。 要使用 WindowsFormsHost 控件,您需要在WPF项目中添加对 WindowsFormsIntegration 程序集的引用。这是如何做的步骤: 在解决方案

    2024年02月12日
    浏览(35)
  • 在WPF应用中使用GongSolutions.WPF.DragDrop实现列表集合控件的拖动处理

    WPF应用中,控件本身也可以通过实现事件代码实现拖动的处理,不过如果我们使用GongSolutions.WPF.DragDrop来处理,事情会变得更加简单轻松,它支持很多控件的拖动处理,如ListBox, ListView, TreeView, DataGrid等源自ItemsControl的控件,本篇随笔介绍在工作流模块中拖动TreeView和DataGrid列表

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包