wpf 列表为空时,按键不可用

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}" />

相关推荐
张人玉3 小时前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
吴可可12319 小时前
C#打断多段线实现方法
c#
Kari111 天前
医药和制造行业的外文文档批量翻译,排版怎么保住?
开发语言·c#·制造
Warson_L2 天前
.NET 8 速成笔记 1:用 VS Code 做 C# / .NET 开发
c#·.net
Warson_L2 天前
.NET 8 速成笔记 9:C# 项目常见场景(基础设施篇)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 8:C# 项目常见业务场景与写法(数据与业务篇)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 3:作业清单
c#·.net
Warson_L2 天前
.NET 8 速成笔记 2:C# / .NET 核心概念(结合本项目代码)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 7:CRUD 速成,10 步加一个新资源
c#·.net
Warson_L2 天前
.NET 8 速成笔记 6:C# 项目结构与依赖管理
c#·.net