(WPF按钮命令绑定)WPF MVVM Button Bind Command

1.xaml按钮设置  Command="{Binding ButtonIncrease}" 这里是命令绑定

       <StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button Content="+"  Command="{Binding ButtonIncrease}" Height="20" Margin="5,-2,0,0" 
                            HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
                    <TextBox  x:Name="tb_BletSpeed" IsEnabled="False" HorizontalAlignment="Left" Margin="10,10,0,0" Text="{Binding BeltSpeed}" 
                              TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>
                    <Button Content="-" Command="{Binding ButtonDecrease}" Height="20" Margin="5,-2,0,0"></Button>
                </StackPanel>

2.编写ViewModel

using InfusionBagSmartLabeler.Common.Command;
using InfusionBagSmartLabeler.Model;
using InfusionBagSmartLabeler.utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;

namespace InfusionBagSmartLabeler.ViewModel
{
    public class NetworkViewModel : INotifyPropertyChanged
    { 
        private int _BeltSpeed = int.Parse(Config.getInstance().getConfig("BeltSpeed"));
        //public ICommand MyCommand => new MyCommand(MyAction, MyCanExec);
        bool isCanExec = true;

        public ICommand ButtonIncrease => new MyCommand(ButtonIncreaseAction, MyCanExec); 
        public ICommand ButtonDecrease => new MyCommand(ButtonDecreaseAction, MyCanExec);
 
 

        public event PropertyChangedEventHandler PropertyChanged;

 

        private bool MyCanExec(object parameter)
        {
            return isCanExec;
        }

        private void ButtonIncreaseAction(object parameter)
        {
            if (BeltSpeed < 100)
            {
                BeltSpeed = BeltSpeed + 1;
            }
          
            Debug.WriteLine($"BeltSpeed {BeltSpeed}"); 
        }

        private void ButtonDecreaseAction(object parameter)
        {
            if (BeltSpeed > 1)
            {
                BeltSpeed = BeltSpeed - 1;
            }

            Debug.WriteLine($"BeltSpeed {BeltSpeed}");
             
        }

    }
}

3.定义MyCommand继承自ICommand 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace InfusionBagSmartLabeler.Common.Command
{
   public class MyCommand :ICommand
    {
        private readonly Action<object> execAction;
        private readonly Func<object, bool> changeFunc;

        public MyCommand(Action<object> execAction,Func<object,bool> changeFunc)
        {
            this.execAction = execAction;
            this.changeFunc = changeFunc;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return this.changeFunc.Invoke(parameter);
        }

        public void Execute(object parameter)
        {
            this.execAction.Invoke(parameter);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值