(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);
        }
    }
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF MVVM中,你可以使用命令模式来将一个按钮的点击事件绑定到ViewModel中的一个方法。以下是在DataGrid中使用按钮绑定命令方法的步骤: 1. 在ViewModel中创建一个ICommand属性,该属性将绑定按钮Command属性。可以使用RelayCommand等现有的ICommand实现,也可以自己实现ICommand接口。如下所示: ```csharp public class MyViewModel { public ICommand MyCommand { get; set; } public MyViewModel() { MyCommand = new RelayCommand(ExecuteMyCommand); } private void ExecuteMyCommand(object parameter) { // 在这里编写命令方法的代码 } } ``` 2. 在XAML中,在DataGrid中创建一个Button列,并将ButtonCommand属性绑定到ViewModel中的命令属性。如下所示: ```xaml <DataGrid ...> <DataGrid.Columns> ... <DataGridTemplateColumn Header="Action"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Do Something" Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> ``` 注意,这里使用了RelativeSource来绑定到Window的DataContext,因为DataGrid的DataContext通常是绑定到ViewModel的。 3. 运行应用程序并单击按钮时,将调用ViewModel中的ExecuteMyCommand方法。 请注意,如果你要在DataGrid中使用按钮,可能需要使用DataGridTemplateColumn来创建一个自定义列。在该列中,使用DataTemplate来定义Button的外观和行为,并将ButtonCommand属性绑定到ViewModel中的命令属性。还可以将ButtonCommandParameter属性绑定到DataGrid中的当前行,以便在命令方法中访问该行的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值