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

与世无争的帅哥 提交于 2019-11-30 09:06:42
Goblin

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>

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}}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!