- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设您有一个按钮,其 command
属性绑定(bind)到一些 ICommand
一些收藏的当前项目。
当集合为null
时,该按钮保持启用状态,点击它似乎是一个空操作。相反,我希望该按钮保持禁用状态。当集合为空时,我想出了以下方法来禁用按钮。然而,对于可以或许以更自然、更简单、更像 MVVM 的方式完成的事情来说,这似乎有点太复杂了。
因此问题来了:是否有更简单的方法来禁用该按钮,最好是在不使用代码隐藏的情况下?
.xaml:
<Button Content="Do something" >
<Button.Command>
<PriorityBinding>
<Binding Path="Items/DoSomethingCmd" />
<Binding Path="DisabledCmd" />
</PriorityBinding>
</Button.Command>
</Button>
.cs:
public class ViewModel : NotificationObject
{
ObservableCollection<Foo> _items;
public DelegateCommand DisabledCmd { get; private set; }
public ObservableCollection<Foo> Items {
get { return _items; }
set { _items = value; RaisePropertyChanged("Items"); }
}
public ViewModel()
{
DisabledCmd = new DelegateCommand(DoNothing, CantDoAnything);
}
void DoNothing() { }
bool CantDoAnything()
{
return false;
}
}
编辑:
一些注意事项:
DoSomethingCmd.CanExecute
做某事将尽一切努力提供帮助,因为没有 DoSomethingCmd
在没有当前项目时访问。DisabledCmd
?我对提升 DoSomethingCmd
不感兴趣因为这不是我要找的。否则我不会问这个问题。另一个编辑:
所以我基本上采用了这个答案作为解决方案:WPF/MVVM: Disable a Button's state when the ViewModel behind the UserControl is not yet Initialized?
我相信,这正是 hbarck 提出的。
最佳答案
我会像 akjoshi 那样做,只是我会使用普通触发器而不是 DataTrigger,并且我会检查 Button.Command 是否为空。由于禁用没有命令的按钮总是有意义的(特别是在没有点击事件处理程序的 MVVM 中),将此触发器包含到按钮的默认样式中也是一个好主意,以便具有此行为在整个应用程序的所有按钮上...我看不出有理由使用虚拟命令。
关于c# - 当没有当前项时,如何禁用绑定(bind)到当前项的 ICommand 的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10648538/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!