WPF Command Binding

Posted ligl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Command Binding相关的知识,希望对你有一定的参考价值。

<Window x:Class="WpfTest.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:WpfTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="190,140,0,0" VerticalAlignment="Top" Width="112" Height="40" 
                Command="{Binding ButtonCom}" CommandParameter="测试数据绑定"/>
    </Grid>
</Window>
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 WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private ICommand _buttonCom = new ButtonCommand();

        public ICommand ButtonCom
        {
            get
            {
                return _buttonCom;
            }

            set
            {
                _buttonCom = value;
            }
        }
    }


    public class ButtonCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            MessageBox.Show(parameter.ToString());
        }
    }

}

 

CommandParameter就是传递的parameter

以上是关于WPF Command Binding的主要内容,如果未能解决你的问题,请参考以下文章

WPF Custom Command And Binding

WPF MVVM,Prism,Command Binding

wpf 中images控件binding显示图片后,图片文件会一直被占用吗?如果是该怎么解

WPF中的命令(Command)

WPF MvvmLight RelayCommand 绑定Command 的使用

wpf程序,binding后界面啥都不显示,下面附上相关代码