欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 56.命令绑定 C#例子 WPF例子

56.命令绑定 C#例子 WPF例子

2025/1/24 18:05:37 来源:https://blog.csdn.net/S13461120713/article/details/145324554  浏览:    关键词:56.命令绑定 C#例子 WPF例子

一共是两个控件,绑定了属性和命令。用的是最简做法

创建依赖:

    public class RelayCommand : ICommand{private readonly Action<object> _execute;public event EventHandler CanExecuteChanged;public RelayCommand(Action<object> execute) => _execute = execute;public bool CanExecute(object parameter) => true; // 总是可执行(简化)public void Execute(object parameter) => _execute(parameter);}

初始化,绑定具体的事件:

        public ICommand MyCommand { get; }public ViewModel(){// 这里假设您有一个按钮点击时要执行的方法叫做 ExecuteMethodMyCommand = new RelayCommand(param => ExecuteMethod());}private void ExecuteMethod(){// 执行您的逻辑System.Windows.MessageBox.Show("Command executed!");Number += 1;}

这里在构造函数中应用了依赖。

绑定到了一个事件,这个事件会在按钮点金时执行。

Mycommand就是前台按钮绑定的属性

ViewModel:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Icommand练习;namespace Icommand练习
{class ViewModel:INotifyPropertyChanged{public ICommand MyCommand { get; }public ViewModel(){// 这里假设您有一个按钮点击时要执行的方法叫做 ExecuteMethodMyCommand = new RelayCommand(param => ExecuteMethod());}private void ExecuteMethod(){// 执行您的逻辑System.Windows.MessageBox.Show("Command executed!");Number += 1;}private int number;public int Number{get {  return number; }set{number = value;OnPropertyChanged(nameof(Number));}}//固定public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string propertyName = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}public class RelayCommand : ICommand{private readonly Action<object> _execute;public event EventHandler CanExecuteChanged;public RelayCommand(Action<object> execute) => _execute = execute;public bool CanExecute(object parameter) => true; // 总是可执行(简化)public void Execute(object parameter) => _execute(parameter);}}

主窗口后台:

using System.Text;
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 Icommand练习
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext = new ViewModel();}}
}

XAML:

<Window x:Class="Icommand练习.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:Icommand练习"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/><Button Command="{Binding MyCommand}" Content="Execute" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Width="75"/></Grid>
</Window>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com