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;
}
}文章来源:https://www.toymoban.com/news/detail-852483.html
<Button Content="My Button" Command="{Binding MyCommand}" />文章来源地址https://www.toymoban.com/news/detail-852483.html
到了这里,关于wpf 列表为空时,按键不可用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!