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

相关推荐
夏霞9 小时前
c# ASP.NET Core SignalR 客户端配置自动重连次数
c#·.netcore
2501_9307077810 小时前
使用C#代码在 Word 文档中查找并替换文本
开发语言·c#·word
一个帅气昵称啊12 小时前
在.NET中使用RAG检索增强AI基于Qdrant的矢量化数据库
ai·性能优化·c#·.net·rag·qdrant
还是大剑师兰特14 小时前
C#面试题及详细答案120道(86-95)-- 进阶特性
c#·大剑师
我是唐青枫16 小时前
C#.NET ControllerBase 深入解析:Web API 控制器的核心基石
c#·.net
O败者食尘D17 小时前
【C#】使用Enigma将Winform或WPF打包成一个exe
c#
The Sheep 202320 小时前
C# 吃一堑,长一智
c#
q***82911 天前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
hixiong1231 天前
C# OpenCVSharp实现Hand Pose Estimation Mediapipe
开发语言·opencv·ai·c#·手势识别