Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable button when its Button.CommandProperty is null

Tags:

binding

wpf

Short explanation

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?

Detailed explanation

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.

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

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>

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. 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).

UPDATE

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

bellow its description.

  1. TextBox is bound to the ViewModel.Data property
  2. Assigning or removing ViewModel into/from Window.DataContext can be done by clicking two buttons - btAssignViewModel and btRemoveViewModel respectively
  3. ViewModel exposes two commands, one of which sets ViewModel.Data to string value, other - sets it to the NULL
  4. These commands are bound to the buttons btSetData & btResetData via their Button.Command properties

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:

  • If btAssignViewModel.DataContext is not null then btAssignViewModel.IsEnabled = false, else true.
  • If btRemoveViewModel.DataContext is null then btRemoveViewModel.IsEnabled = false, else true.
  • If ViewModel.Data is null then btSetData.IsEnabled = true, else false.
  • If ViewModel.Data is null then btResetData.IsEnabled = false, else true.

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.

like image 891
JSP Avatar asked Nov 09 '10 20:11

JSP


People also ask

How do you disable a button with a condition?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .


1 Answers

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>
like image 71
Goblin Avatar answered Sep 18 '22 08:09

Goblin