本文介绍了如何禁用按钮时的Button.CommandProperty为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Button.CommandProperty势必SomeObject.SomeCommand视图模型的财产。当SomeObject的SomeCommand属性设置为空或整个SomeObject属性被设置为null,该按钮保持启用状态。如何能在按钮下方此情况下被禁用?

Button.CommandProperty is bound to SomeObject.SomeCommand property of the ViewModel. When SomeCommand property of SomeObject is set to null or entire SomeObject property is set to null, this button remains enabled. How can the button be disabled under this circumstances?

我使用的MVVM,其行为就像一个浏览器创建应用程序:
主view_model(其对应于主窗口视图)具有工作区view_models的列表。每个工作区view_model对应的TabPage在窗口的TabControl的。
主要view_model具有对应当前活动的TabPage CurrentWorkspace属性。

I am creating application using MVVM, which behaves like a browser: Main view_model (which corresponds to main window as view) has a list of Workspace view_models. Each Workspace view_model corresponds to TabPage in windows's TabControl.Main view_model has CurrentWorkspace property which corresponds currently active TabPage.

在哪里是带按钮的工具栏,它采用了CurrentWorkspace提供的命令主窗口。例如,访问重装工作区数据实现为:

In main window where is a toolbar with buttons, which utilizes commands provided by the CurrentWorkspace. For example, access to reloading workspace data is realized as:

<Button Name="btReload" Content="Reload" 
        Command="{Binding Path=CurrentWorkspace.ReloadCommand, UpdateSourceTrigger=PropertyChanged}"/>

我试图通过创建DataTriggers完成按钮禁用的任务,但似乎触发作品只有一次,没有更多:

I tried to accomplish the task of button disabling by creating DataTriggers, but it seems that triggers works only first time and no more:

<Button Name="btReload" Content="Reload" 
        Command="{Binding Path=CurrentWorkspace.ReloadCommand, UpdateSourceTrigger=PropertyChanged}">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CurrentWorkspace, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                     <Setter Property="dxb:BarButtonItem.IsEnabled" Value="False"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=CurrentPage.CurrentWorkspace, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                    <Setter Property="dxb:BarButtonItem.IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

它看起来真的很愚蠢:如MS Word中使用documentless客户区,并在同一时间,在工具栏上有很多现成的点击按钮(带格式化和其他文档特定的功能)。请帮助我,:)

it looks really stupid: like MS Word with documentless client area and at the same time with a lot of ready-for-click buttons in toolbar (with formatting and other document-specific features). Please, help me, :)

P.S。当添加到工具栏按钮与绑定到CurrentWorkspace DataContext的,那么它的DataContextChanged仅事件触发正确激活或添加或移除窗口工作区选项卡时。那么,问题在什么地方DataTrigger(或查看一般),而不是在它的视图模型(S)。

P.S. When adding to toolbar a button with DataContext binded to CurrentWorkspace, then its DataContextChanged event fires properly when activating or adding or removing workspace tab in the window. So, the problem somewhere in DataTrigger (or in View as generally), not in it's ViewModel(s).

我上传示例项目在VS2010,链接存档:

I uploaded sample project on VS2010, link for the archive: http://www.filefactory.com/file/b43455e/n/WhatIfCommandIsNull.rar

娄其描述。


  1. 文本框被绑定到ViewModel.Data财产

  2. 分配或删除视图模型到从Window.DataContext /可以通过单击两个按钮完成 - 分别btAssignViewModel和btRemoveViewModel

  3. 视图模型公开了两个命令,其中一个设置为ViewModel.Data字符串值,等 - 将其设置为空

  4. 这些命令绑定到按钮btSetData&安培;通过他们的Button.Command性能btResetData

正如你所看到的,当Window.DataContext设置为视图模型的实例,既正常工作的命令,&安培; ResetDataCommand.CanExecute是工作压力太大时(ViewModel.Data为NULL,ResetDataCommand.CanExecute返回false&放按钮btResetData被禁用)。一旦Window.DataContext被设置为null,最后两个按钮启用(前两个那些trere没有命令绑定)。

As you can see, when Window.DataContext is set to the ViewModel instance, both the commands working properly, & ResetDataCommand.CanExecute is working too (when ViewModel.Data is NULL, ResetDataCommand.CanExecute returns false & button btResetData is disabled). Once the Window.DataContext is set to null, last two buttons enables (for the first two ones trere are no commands are bound).

问题是要实现声明(通过触发器)接下来的四个规则:

The problem is to realize declaratively (via triggers) next four rules:


  • 如果btAssignViewModel.DataContext不为空则btAssignViewModel.IsEnabled = false,其他真实的。

  • 如果btRemoveViewModel.DataContext为null,则btRemoveViewModel.IsEnabled = false,其他真实的。

  • 如果ViewModel.Data为null,则btSetData.IsEnabled = true,否则为false。

  • 如果ViewModel.Data为null,则btResetData.IsEnabled = false,其他真实的。

我认为,前两个规则可以使用触发器,后两个来实现 - 使用DataTriggers。但他们没有工作,所以我抹去他们的项目。

I think that first two rules can be realized using Triggers, second two - using DataTriggers. But they aren't working, so I erased them from the project.

推荐答案

(等待您的情况),这可能会实现

This might work (pending your circumstances)

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger Property="Command" Value="{x:Null}">
               <Setter Property="IsEnabled" Value="false"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

这篇关于如何禁用按钮时的Button.CommandProperty为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:56