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 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
YuanlongWang3 小时前
C# 中,依赖注入(DI)的实现方式
c#
SmartSoftHelp开发辅助优化5 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#
future_studio7 小时前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
c#上位机8 小时前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
玩泥巴的10 小时前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作
SunnyDays101111 小时前
C# 实现高保真 Excel 转 PDF(无需 Office 环境)
经验分享·c#·excel转pdf
攻城狮CSU11 小时前
C# 数据加载专题 之泛型序列化
java·servlet·c#
爱编程的鱼11 小时前
C# 参数详解:从基础传参到高级应用
开发语言·microsoft·c#
流水线上的指令侠13 小时前
使用C#写微信小程序后端——电商微信小程序
微信小程序·小程序·c#·visual studio