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

相关推荐
小羊没烦恼!1 小时前
关于大型asp.net应用系统的架构-架构的选择
java·服务器·开发语言·前端·c#
小羊没烦恼!19 小时前
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
java·开发语言·前端·数据库·算法·c#
UIU1141 天前
头文件可以防什么错?运用头文件做声明
c++·学习·c#
鑫 源1 天前
C#源码开源, 获取macOS 电池数据,包括iOS (xamarin开发)
c#·.net·xamarin
爱吃奥利奥_wen2 天前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
wflynn2 天前
GitHub 今日推荐|SCSKiller:游戏着色器预编译消除卡顿
开源·c#·github·nvidia·amd·directx12
疯狂的维修3 天前
关于用C#写电机起保停控制
开发语言·c#
用户788477316343 天前
门店核销闭环:小程序扫码 + .NET 幂等接口 + WPF 看板,一天能跑通
微信小程序·c#
samble3 天前
从零搭建灌装监控系统(十三):报警系统设计,生命周期与通知
c#·wpf·mvvm·modbus·工业控制
miofly3 天前
GitHub 今日推荐|SCSKiller:游戏着色器预编译消除卡顿
开源·c#·github