首页 > 编程语言 >WPF Button Command and CommandParamet completed via C# no xaml at all

WPF Button Command and CommandParamet completed via C# no xaml at all

时间:2024-08-25 18:25:56浏览次数:9  
标签:via no C# System private Windows using btn public

  private void ButtonCommandBinding()
  {
      Button btn = new Button();
      btn.Width = 500;
      btn.Height = 300;
      btn.Content = "Show Info";
      btn.Background = new SolidColorBrush(Colors.Blue); 
      btn.Command = ShowCmd;
      btn.CommandParameter = this;
      this.Content = btn;
  }

 

 

 

 

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 WpfApp284
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            InitCmds();
            ButtonCommandBinding();
        }

        private void InitCmds()
        {
            ShowCmd = new DelCmd(ShowCmdExecuted);
        }

        private void ShowCmdExecuted(object obj)
        {
            var win = obj as Window;
            if(win!=null)
            {
                MessageBox.Show($"Now is {DateTime.Now.ToString("yyyyMMddHHmmffff")},Width:{win.ActualWidth},Height:{win.ActualHeight}");
            }
        }

        private void ButtonCommandBinding()
        {
            Button btn = new Button();
            btn.Width = 500;
            btn.Height = 300;
            btn.Content = "Show Info";
            btn.Background = new SolidColorBrush(Colors.Blue); 
            btn.Command = ShowCmd;
            btn.CommandParameter = this;
            this.Content = btn;
        }

        public DelCmd ShowCmd { 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,no,C#,System,private,Windows,using,btn,public
From: https://www.cnblogs.com/Fred1987/p/18379283

相关文章

  • (十八)Flink CEP 详解
    目录FlinkCEP引用模式API单个模式组合模式模式组匹配后跳过策略检测模式从模式中选取CEP库中的时间实用案例FlinkCEP是在Flink上层实现的复杂事件处理库。它可以让你在无限事件流中检测出特定的事件模型,有机会掌握数据中重要的部分。FlinkCEP主要用于实......
  • C/C++、Java、Python全面比较
    以下是对C/C++、Java、Python三种编程语言的全面比较,以表格形式呈现:特性/语言C/C++JavaPython类型系统静态类型静态类型动态类型内存管理手动管理自动管理(垃圾回收)自动管理(垃圾回收)编译/解释编译型编译型(通过JVM解释执行)解释型性能高(直接编译成机器码)中等(通过JIT优化)较低(解......
  • 【Node】【4】事件循环和EventEmitter类
    事件循环事件驱动:node中程序的执行是由事件的发生和相应的事件处理器(eventHandler)来驱动的编程范式。程序监听并响应发生的事件。每个异步事件都生成一个事件观察者,在执行某个事件(主题)结束的时候则触发这个事件上的处理函数(观察者)。【让我瞅瞅什么时候开始执行,例如点击事......
  • 56个JavaScript 实用工具函数助你提升开发效率!
    今天来看看JavaScript中的一些实用的工具函数,希望能帮助你提高开发效率!整理不易,如果觉得有用就点个关注鼓励一下吧!1.数字操作(1)生成指定范围随机数export const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;(2)数字千分......
  • Python3.11二进制AI项目程序打包为苹果Mac App(DMG)-应用程序pyinstaller制作流程(App
    众所周知,苹果MacOs系统虽然贵为Unix内核系统,但由于系统不支持N卡,所以如果想在本地跑AI项目,还需要对相关的AI模块进行定制化操作,本次我们演示一下如何将基于Python3.11的AI项目程序打包为MacOS可以直接运行的DMG安装包,可以苹果系统中一键运行AI项目。MacOs本地部署AI项目首先确......
  • Win32基于Refs实现 Copy On Write
    Refs相对Ntfs来说,有一个很重要的技术BlockClone。块克隆指令文件系统代表应用程序复制一段文件字节,目标文件可能与源文件相同,也可能不同。不幸的是,传统的复制操作成本高昂,因为它们会触发对底层物理数据的昂贵读写操作。然而,在ReFS中,块克隆执行的是低成本元数据操作,而不是读取......
  • 亲测好用,吐血整理 ChatGPT 3.5/4.0 新手使用手册~ 【2024.08.25 更新】
    废话不多说,直接分享正文~以下是小编为大家搜集到的最新的ChatGPT国内站,各有优缺点。1、AIPlus(稳定使用)推荐指数:⭐⭐⭐⭐⭐     yixiaai.com该网站已经稳定运营了1年多了。2023年3月份第一批上线的网站。网站支持GPT-3.5、4.0及4o、4omini模型,手机和电脑都能用......
  • 【xilinx】解决 I/O 时钟布局器错误:UltraScale 示例
    示例详细信息:设备: xcvu9p-flga2104-2-e问题:尽管使用GCIO引脚作为时钟,但该工具仍返回I/OClockPlacer错误错误:<spanstyle="background-color:#f3f3f3"><spanstyle="color:#333333"><code>ERROR:[Place30-675]Sub-optimalplacementforaglobalcloc......
  • 一种使用树结构优化switch语句的方法
    介绍在日常装B写代码中会经常用到ifelseif语句如:if(1){ } elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1){ //TODO }elseif(1)......
  • WPF LogicalTree vs Visual Tree
    Copyfrom https://www.c-sharpcorner.com/blogs/wpf-logical-and-visual-trees1  WPF'shierarchicalstructurerequiresanewconceptualmodelofapplicationstructure,whichtakestheformofanelementtree.Twotypesofelementtreesarerequiredt......