<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,类文件内容如下:文章来源:https://www.toymoban.com/news/detail-732432.html
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模板网!