首页 > 其他分享 >WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior

WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior

时间:2024-10-02 21:11:33浏览次数:1  
标签:via MVVM show Windows win System using public DelCmd

1.Install Microsoft.Xaml.Behaviors.Wpf

 

 

2.

 <behavior:Interaction.Triggers>
     <behavior:EventTrigger EventName="MouseDown">
         <behavior:InvokeCommandAction 
             Command="{Binding ClickCommand}"
             CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
             AncestorType={x:Type Window}}}"/>
     </behavior:EventTrigger>
 </behavior:Interaction.Triggers>


//cs
private void InitCommands()
{
    ClickCommand = new DelCmd(ClickCommandExecuted);
}

private void ClickCommandExecuted(object obj)
{
     var win = obj as Window;
    if (win != null)
    {
        var pt = Mouse.GetPosition(win);
        MessageBox.Show($"{pt.X},{pt.Y}", "Clicked Position!");
    }
}

public DelCmd ClickCommand { get; set; }

 

 

 

 

 

 

 

 xaml

//xaml
<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="MouseDown">
            <behavior:InvokeCommandAction 
                Command="{Binding ClickCommand}"
                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                AncestorType={x:Type Window}}}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>
    <Window.Resources>
        <SolidColorBrush Color="Cyan" x:Key="imgBg"/>
    </Window.Resources>
    <Window.DataContext>
        <local:BookVM/>
    </Window.DataContext>     
    <Grid>
        <Image Source="/Images/1.jpg" RenderOptions.BitmapScalingMode="Fant"/>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class BookVM
    {
        public BookVM()
        {
            InitCommands();
        }

        private void InitCommands()
        {
            ClickCommand = new DelCmd(ClickCommandExecuted);
        }

        private void ClickCommandExecuted(object obj)
        {
             var win = obj as Window;
            if (win != null)
            {
                var pt = Mouse.GetPosition(win);
                MessageBox.Show($"{pt.X},{pt.Y}", "Clicked Position!");
            }
        }

        public DelCmd ClickCommand { get; set; }
    }

    public class DelCmd : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }


        private Action<object> execute;

        private Predicate<object> canExecute;

        public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue)
        {
            execute = executeValue;
            canExecute = canExecuteValue;
        }

        public DelCmd(Action<object> executeValue) : this(executeValue, null)
        {
        }

        public bool CanExecute(object parameter)
        {
            if (canExecute == null)
            {
                return true;
            }
            return canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }
}

 

标签:via,MVVM,show,Windows,win,System,using,public,DelCmd
From: https://www.cnblogs.com/Fred1987/p/18445087

相关文章

  • WPF MVVM behavior CallMethodAction InvokeCommandAction
    1.Install  Microsoft.Xaml.Behaviors.Wpf 2.xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMet......
  • WPF MVVM入门系列教程(二、依赖属性)
    说明:本文是介绍WPF中的依赖属性功能,如果对依赖属性已经有了解了,可以浏览后面的文章。 为什么要介绍依赖属性在WPF的数据绑定中,密不可分的就是依赖属性。而MVVM又是跟数据绑定紧密相连的,所以在学习MVVM之前,很有必须先学习一下依赖属性。 依赖属性(DepencencyProperty)是什......
  • WPF ProgressBar show value
    //xaml<Windowx:Class="WpfApp424.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • ctfshow-web-信息搜集(11-17)
    web11题目提示:域名其实也可以隐藏信息,比如ctfshow.com就隐藏了一条信息。原理:通过Dns检查查询Flag。这里可以用阿里云的网站:Dns查询网站:阿里云网站运维检测平台(aliyun.com)web12题目提示:有时候网站上的公开信息,就是管理员常用密码原理:查看robots.txt文件,找到后台登录页......
  • 基于CTFshow的文件上传二次渲染绕过与CTF实战
    1.二次渲染简介二次渲染指的是上传的文件(如图片),为了显示的更加规范(尺寸、像素),网站会对文件进行二次处理,经过解码或转换可能导致其中的恶意代码失效。例如,后门程序在图像渲染过程中可能被清除或无法执行。2.二次渲染存在性判断2.1文件大小变化访问上传的文件地址,重新下载下......
  • show processlist和show full processlist说明
    showprocesslist和showfullprocesslistprocesslist命令的输出结果显示了有哪些线程在运行,不仅可以查看当前所有的连接数,还可以查看当前的连接状态帮助识别出有问题的查询语句等。如果是root帐号,能看到所有用户的当前连接。如果是其他普通帐号,则只能看到自己占用的连接。showp......
  • Verba - Weaviate RAG 私人助理
    文章目录一、关于Verba什么是Verba?功能列表二、Verba入门安装部署三、API密钥1、Weaviate2、Ollama3、UNSTRUCTURED4、AssemblyAI5、OpenAI6、HuggingFace四、如何使用pip进行部署五、如何从源代码构建六、如何使用Docker安装VerbaVerbaWalkthrough选择您的部署导入......
  • mvvm软件架构 个人见解
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMVVM{  //MVVM框架——Vm层——///  ///主体框架是 数据 ViewModel---》dataservice----》config-----》各个模块  //......
  • CTFSHOW pwn03 WrriteUp
    本文来自一个初学CTF的小白,如有任何问题请大佬们指教!题目来源CTFShowpwn-pwn03(ret2libc)https://ctf.show/challenges思路1.下载题目放到checksec先查一下2.IDA打开题目Shift+F12查看字符串发现没有system和/bin/sh,但是有libc文件。3.用gdb的cyclic查询一下溢出所......
  • WPF Image show picture in high resolution periodically via System.Timers.Timer
    <Windowx:Class="WpfApp411.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......