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

相关推荐
淡海水17 小时前
09-04-内存-Unity原生对象生命周期
unity·c#·游戏引擎·gc
吴可可12320 小时前
富怡CAD二次开发指南
c#
1-1=020 小时前
C# IO和序列化
c#·.net·.netcore
寂寞的船21 小时前
【Unity Bug挑战】第10期 SerializeReference 内存泄漏之谜
unity·c#·游戏引擎·bug
CSharp精选营1 天前
从控制台到「会动手」的 AI:用 Semantic Kernel 搭应用(小白教程)
c#·.net·ai agent·semantickernel·functioncalling
宝桥南山2 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=02 天前
C# 中 Lambda、Linq 与 Expression
c#·linq
zhanghaha13142 天前
C# 系列教程_7 判断语句(分支)保姆级精讲
开发语言·c#
zhanghaha13142 天前
C# 系列教程_8 循环语句 保姆级精讲(面向新手)
开发语言·c#
rockey6272 天前
AScript之LINQ扩展
c#·.net·script·动态脚本