wpfbutton按钮禁用_WPF MVVM命令可以执行启用/禁用按钮

I want to enable RibbonButton when textbox property text isn't null. Disable RibbonButton when textbox property text is null. I want to use CanExecute method in ICommand for it. How can I do it?

View:

LargeImageSource="..\Shared\img\save_diskete.png"

Label="Save"

Command="{Binding ButtonCommand}">

ViewModel

class KomentarViewModel:BaseViewModel

{

#region Data

private ICommand m_ButtonCommand;

public ICommand ButtonCommand

{

get

{

return m_ButtonCommand;

}

set

{

m_ButtonCommand = value;

}

}

private string textKomentar;

public string TextKomentar

{

get

{

return this.textKomentar;

}

set

{

// Implement with property changed handling for INotifyPropertyChanged

if (!string.Equals(this.textKomentar, value))

{

textKomentar = value;

OnPropertyChanged("TextKomentar");

}

}

}

private ObservableCollection allCommentsInc;

public ObservableCollection AllCommentsInc

{

get

{

return allCommentsInc;

}

set

{

allCommentsInc = value;

OnPropertyChanged("AllCommentsInc");

}

}

public int idIncident { get; private set; }

public Incident incident { get; private set; }

#endregion

#region Constructor

public KomentarViewModel(int id)

{

CC_RK2Entities context = new CC_RK2Entities();

this.idIncident = id;

AllCommentsInc = new ObservableCollection(context.Komentar.Where(a => a.Incident_id == idIncident));

incident = context.Incident.Where(a => a.id == idIncident).First();

//ButtonCommand = new RelayCommand(new Action(ShowMessage));

}

#endregion

#region Methods

//ukaz napsany text

public void ShowMessage(object obj)

{

//MessageBox.Show(obj.ToString());

MessageBox.Show(this.TextKomentar);

}

}

RelayCommand

namespace Admin.Shared.Commands

{

class RelayCommand : ICommand

{

private Action _action;

public RelayCommand(Action action)

{

_action = action;

}

#region ICommand Members

public bool CanExecute(object parameter)

{

return true;

}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)

{

_action(parameter);

}

#endregion

}

}

解决方案

You need to modify your RelayCommand class like this

class RelayCommand : ICommand

{

private Action _action;

private Func _func;

public RelayCommand(Action action,Func func)

{

_action = action;

_func = func;

}

public void RaiseCanExecuteChanged()

{

if(CanExecuteChanged!=null)

CanExecuteChanged(this,new EventArgs());

}

#region ICommand Members

public bool CanExecute(object parameter)

{

if (_func != null)

return _func();

return true;

}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)

{

_action(parameter);

}

#endregion

}

Initialize ButtonCommand as

ButtonCommand = new RelayCommand((s) => ShowMessage(s),()=>!string.IsNullOrEmpty(TextKomentar));

RaiseCanExcuteChanged from the setter of Text property

public string TextKomentar

{

get

{

return this.textKomentar;

}

set

{

// Implement with property changed handling for INotifyPropertyChanged

if (!string.Equals(this.textKomentar, value))

{

textKomentar = value;

OnPropertyChanged("TextKomentar");

}

ButtonCommand.RaiseCanExecuteChanged();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值