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管理包,检索包的名称,不然就会保持

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值