Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF/MVVM: Disable a Button's state when the ViewModel behind the UserControl is not yet Initialized?

I have a DocumentListView.Xaml with a ListBox and 3 Buttons.

Behind that UserControl sits a DocumentListViewModel with 3 Buttons and their Command Property bound to 3 RelayCommands.

I have 3 Controller like AdministrationController, BillingController, ReportController.

Every Controller has ObservableCollections like Customer 1 : N Order 1: N Document same for the other Controller.

In one Controller I have a special binding situation. When my DocumentListViewModel is not initialized by its parent ViewModel like OrderViewModel (because no orders are loaded/exist) then my UserControl has 3 buttons which are ENABLED. Ok the user can press the 3 buttons and nothing happens but still its very confusing and above all the consistency in my user interface is gone.

How can I set the Command of a Button as default to "Disabled" ?

Setting the Buttons IsEnabled property to false does not help because the button will stay forever in the disabled state. No CanExecute TRUE will set it to IsEnabled = true.

AND I do not want to introduce another property IsButtonEnabled... that stupid because then I have both worlds winforms and wpf behind my buttons logic... ICommand should be enough.

like image 742
Elisabeth Avatar asked Dec 12 '10 20:12

Elisabeth


1 Answers

Or you can use a Style for the button to disable:

<Style TargetType="{x:Type Button}" x:Key="DisablerButton">
    <Style.Triggers>
        <Trigger Property="Command" Value="{x:Null}">
            <Setter Property="IsEnabled" Value="False" />
        </Trigger>
    </Style.Triggers>
</Style>
like image 76
Goblin Avatar answered Sep 24 '22 01:09

Goblin