赞
踩
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;
public class MyViewModel : BindableBase
{
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get => _items;
set => SetProperty(ref _items, value);
}
public DelegateCommand MyCommand { get; private set; }
public MyViewModel()
{
Items = new ObservableCollection<string>(); // 初始化列表
MyCommand = new DelegateCommand(ExecuteMyCommand, CanExecuteMyCommand);
// 当列表内容变化时,触发CanExecute条件的检查
Items.CollectionChanged += (s, e) => MyCommand.RaiseCanExecuteChanged();
}
private void ExecuteMyCommand()
{
// 按钮点击时执行的操作
}
private bool CanExecuteMyCommand()
{
// 列表为空时,命令不可执行,按钮不可用
return Items.Count > 0;
}
}
<Button Content="My Button" Command="{Binding MyCommand}" />
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。