gpt4 book ai didi

c# - WPF MVVM 命令可以执行启用/禁用按钮

转载 作者:太空狗 更新时间:2023-10-29 18:14:48 26 4
gpt4 key购买 nike

我想在文本框属性文本不为空时启用 RibbonButton。当文本框属性文本为空时禁用 RibbonButton。我想在 ICommand 中为此使用 CanExecute 方法。我该怎么做?

查看:

 <Custom:RibbonButton
LargeImageSource="..\Shared\img\save_diskete.png"
Label="Save"
Command="{Binding ButtonCommand}">
</Custom:RibbonButton>

View 模型

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<Komentar> allCommentsInc;
public ObservableCollection<Komentar> 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<Komentar>(context.Komentar.Where(a => a.Incident_id == idIncident));
incident = context.Incident.Where(a => a.id == idIncident).First();

//ButtonCommand = new RelayCommand(new Action<object>(ShowMessage));
}
#endregion

#region Methods
//ukaz napsany text
public void ShowMessage(object obj)
{
//MessageBox.Show(obj.ToString());
MessageBox.Show(this.TextKomentar);
}
}

中继命令

namespace Admin.Shared.Commands
{
class RelayCommand : ICommand
{
private Action<object> _action;

public RelayCommand(Action<object> 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
}
}

最佳答案

您需要像这样修改您的 RelayCommand 类

  class RelayCommand : ICommand
{
private Action<object> _action;
private Func<bool> _func;

public RelayCommand(Action<object> action,Func<bool> 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
}

将 ButtonCommand 初始化为

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

RaiseCanExcuteChanged 来自 Text 属性的 setter

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

关于c# - WPF MVVM 命令可以执行启用/禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com