WPF/MVVM:当UserControl后面的ViewModel尚未初始化时,禁用Button的状态?

Eli*_*eth 9 wpf button icommand

我有一个带有ListBox和3个按钮的DocumentListView.Xaml.

在UserControl后面是一个带有3个按钮的DocumentListViewModel,它们的Command属性绑定到3个RelayCommands.

我有3个Controller,如AdministrationController,BillingController,ReportController.

每个Controller都有ObservableCollections,如客户1:N订单1:N文档与其他控制器相同.

在一个控制器中,我有一个特殊的绑定情况.当我的DocumentListViewModel未被其父ViewModel(如OrderViewModel)初始化时(因为没有加载/存在订单),那么我的UserControl有3个按钮,它们是ENABLED.好的,用户可以按下3个按钮,没有任何反应,但仍然非常混乱,最重要的是我的用户界面的一致性消失了.

如何将按钮的命令设置为默认为"已禁用"?

将按钮IsEnabled属性设置为false没有帮助,因为该按钮将永远保持在禁用状态.否CanExecute TRUE会将其设置为IsEnabled = true.

而且我不想引入另一个属性IsButtonEnabled ...那个愚蠢因为那时我的两个世界winforms和wpf在我的按钮逻辑之后...... ICommand应该就够了.

Gob*_*lin 19

或者您可以使用按钮样式禁用:

<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>
Run Code Online (Sandbox Code Playgroud)


Jos*_*osh 5

这是一个有趣的情况.老实说,我从来没有碰到过加载UI和交互式的情况,但ViewModel还没有绑定.

但是,暂时忽略这一点,您可能会在绑定上使用FallbackValue绑定到全局可用的NullCommand或者总是为其CanExecute方法返回false的内容.

<Button Command="{Binding SaveCommand, FallbackValue={StaticResource NullCommand}}" />
Run Code Online (Sandbox Code Playgroud)