MVVM将IsEnabled绑定到ViewModel中的多个bool

Dar*_*ter 1 c# mvvm visual-studio

我有下面的按钮,它的IsEnabled属性绑定到ViewModel中名为EnableBtn的bool.

如果我有另一个叫做EnableMail的bool,我该如何修改它以便IsEnabled被绑定到两者?

    <Button IsEnabled="{Binding EnableBtn, Converter={StaticResource InvertBooleanConverter}}" x:Name="SaveSendButton" Grid.Row="0" Grid.Column="1" Text="{i18n:Translate SaveAndSend}" Style="{StaticResource bottomButtonsBlue}" Command="{Binding EmailPlanCommand}"></Button>
Run Code Online (Sandbox Code Playgroud)

小智 5

    public bool IsBothEnabled
    {
        get
        {
            if (EnableBtn && EnableMail)
                return true;
            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在将Button.IsEnabled属性绑定到IsBothEnabled.

  • 请注意,如果其中一个属性"EnableBtn"或"EnableMail"发生更改,则必须调用PropertyChanged("IsBothEnabled"). (3认同)