wpf 按钮 IsEnabled 绑定无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28874883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:59:36  来源:igfitidea点击:

Button IsEnabled binding not working properly

c#wpfxaml

提问by MikroDel

Button.IsEnableddoesn't work properly.

Button.IsEnabled不能正常工作。

I have debugged the code and the setter for the property was hit with the "true" value. But the button is still disabled.

我已经调试了代码,并且属性的 setter 被命中了“true”值。但该按钮仍处于禁用状态。

View.xaml:

查看.xaml:

<StackPanel Grid.Row="2" Margin="0,20,0,0" >
        <Button Name="ButtonOk" Content="OK" Margin="0,0,4,0" IsEnabled="{Binding SomethingIsValid}"  Command="{Binding CommandOk}" />
        <Button Name="ButtonCancel" Content="Cancel" Margin="0,0,4,0" IsCancel="True" /
</StackPanel>

View.xaml.cs:

查看.xaml.cs:

...
public View(ViewModel viewModel)
{
    this.InitializeComponent();

    this.viewModel = viewModel;
    this.DataContext = viewModel;            
}

ViewModel:

视图模型:

public bool SomethingIsValid
{
   get
   {
      return somethingIsValid;
   }
   set
   {
      this.somethingIsValid= value;
      this.RaisePropertyChanged(() => this.SomethingIsValid);
   }
}

#region IDataErrorInfo
public string this[string columnName]
{
   get
   {
      this.SomethingIsValid= false;

      if ("SomeName" == columnName)
      {
         if (string.IsNullOrEmpty(this.Status))
         {
            return "Please bla bla..";
         }
      }

      this.SomethingIsValid = true;
      return string.Empty;
   }
}

public string Error
{
   get
   {
      return string.Empty;
   }
}
#endregion

public ICommand CommandOk
{
   get
   {
      if (this.commandOk == null)
      {
         this.commandOk = new RelayCommand(this.CommandOkAktion, () => this.SomethingIsValid );
      }

      return this.commandOk;
   }
}

回答by Martin Moser

If you are using a command, it's not a good idea to separately bind the IsEnabled property of the button. Instead you should provide the correct value in the "CanExecute" of the command. This will enable the button as well.

如果您正在使用命令,单独绑定按钮的 IsEnabled 属性不是一个好主意。相反,您应该在命令的“CanExecute”中提供正确的值。这也将启用按钮。

回答by Anik Saha

If anyone is still having trouble with this - make sure that your IsEnabled property comes after the Command property.

如果有人仍然遇到此问题 - 请确保您的 IsEnabled 属性位于 Command 属性之后。

<Button Command="{Binding StartCommand}" IsEnabled="{Binding StartEnabled}" />

- works

- 作品

  <Button IsEnabled="{Binding StopEnabled}" Command="{Binding StopCommand}"  />

- does not work

- 不起作用

Note that both of my commands can always execute. An easier fix for me.

请注意,我的两个命令始终可以执行。对我来说更容易修复。

回答by Bolu

Form what I can see in your posted code, if nothing calls this["SomeName"], your SomethingIsValidwill always be false, and that is why your button is showing disabled. So my suggestion/solution is:

形成我在您发布的代码中可以看到的内容,如果没有调用this["SomeName"],您SomethingIsValid将始终是false,这就是为什么您的按钮显示disabled。所以我的建议/解决方案是:

Remove IsEnabledbinding from xaml (as you won't need this if your Command binding is working correctly):

IsEnabled从 xaml 中删除绑定(因为如果您的命令绑定工作正常,您将不需要它):

<StackPanel Grid.Row="2" Margin="0,20,0,0" >
    <Button Name="ButtonOk" Content="OK" Margin="0,0,4,0" Command="{Binding CommandOk}" />
    <Button Name="ButtonCancel" Content="Cancel" Margin="0,0,4,0" IsCancel="True" /
 </StackPanel> 

Change your ViewModel code to something like below (your current Command definition is OK, so no change to that part). The code below makes sure when executing CanCommandExecute, it will re-examine SomethingIsValid:

将您的 ViewModel 代码更改为如下所示的内容(您当前的命令定义没问题,因此无需更改该部分)。下面的代码确保在执行时CanCommandExecute,它会重新检查SomethingIsValid

public bool SomethingIsValid
{
   get
   {
      return string.IsNullOrEmpty(Error);
   }
}

public string this[string columnName]
{
   get
   {
      switch(columnName)
      {
          case "SomeName":
          {
               if (string.IsNullOrEmpty(this.Status))
               {
                  return "Please bla bla..";
               }
               break;
          }
          case "SomeOtherName":
          {
               if (string.IsNullOrEmpty(this.OtherProperty))
               {
                  return "Please bla bla..";
               }
               break;
          }

      }
      return string.Empty;         

   }
}

public string Error
{
   get
   {
      return this["SomeName"]+this["SomeOtherName"];
   }
}
#endregion

回答by Robin

You have to implement the INotifyPropertyChanged interface in your ViewModel, so the view gets notfied when the property gets updated.

您必须在 ViewModel 中实现 INotifyPropertyChanged 接口,以便在属性更新时通知视图。

Have a look at this: https://msdn.microsoft.com/en-US/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

看看这个:https: //msdn.microsoft.com/en-US/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

回答by Lenka

If you are using RelayCommand, you can use the second parameter as boolean method which indicates whether command can be executed:

如果您使用 RelayCommand,您可以使用第二个参数作为布尔方法,指示是否可以执行命令:

public RelayCommand(Action execute, Func<bool> canExecute);

Usage:

用法:

this.DetermineCommand = new RelayCommand(this.Determine, () => this.IsValid);

回答by James Wierzba

I solved the issue by binding the value of IsEnabled. Explicitly setting it to False did not work.

我通过绑定 IsEnabled 的值解决了这个问题。将其显式设置为 False 不起作用。

This worked:

这有效:

<Button Text="Login" Command="{Binding LoginCommand}" IsEnabled="{Binding ButtonsEnabled}" />

This did not work:

这不起作用:

<Button Text="Login" Command="{Binding LoginCommand}" IsEnabled="False" />