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

前端 未结 2 1269
臣服心动
臣服心动 2020-12-31 14:57

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

相关标签:
2条回答
  • 2020-12-31 15:15

    This is an interesting situation. Honestly I've never run into the case where the UI was loaded and interactive but the ViewModel was not yet bound.

    However, ignoring that for a moment, you could potentially use a FallbackValue on your binding to bind to a globally available NullCommand or something that always returns false for its CanExecute method.

    <Button Command="{Binding SaveCommand, FallbackValue={StaticResource NullCommand}}" />
    
    0 讨论(0)
  • 2020-12-31 15:33

    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>
    
    0 讨论(0)
提交回复
热议问题