简体   繁体   English

如何在xaml中使用ICommand更改按钮的标签?

[英]How can I change the label of the button using ICommand in xaml?

I have the following button in my view Home.xaml. 我的视图Home.xaml中具有以下按钮。 I have it binding to a property called StartStopLabel. 我将其绑定到名为StartStopLabel的属性。 I have implemented the interface ICommand in the same view and I could able to change the label to text "Stop" after clicking on Start(which is the initial status which I am setting in the constructor of the view as this.StartStopLabel="Start",this.ButtonStatus="click on start button") , but I am not able to do the reverse that is changing the label of button from "Stop" to "Start" .What I mean to say is ICommand is not notified of the click event when the button label shows "Stop". 我已经在同一视图中实现了ICommand接口,并且单击Start(在视图的构造函数中将其设置为this.StartStopLabel="Start",this.ButtonStatus="click on start button")的初始状态)后,便可以将标签更改为文本“ Stop”。 this.StartStopLabel="Start",this.ButtonStatus="click on start button") ,但是我无法做到将按钮的标签从” Stop“更改为” Start“的反向操作。我的意思是未通知ICommand按钮标签显示“停止”时的单击事件。

Once user clicks on the "stop" button(ie when the button label shows the text "stop" ) I want to change the text of the textblock "BtnSTatus" to "You have clicked on start button" and back to "Click on Start button" when the button label again shows text "Start". 一旦用户单击“停止”按钮(即按钮标签显示文本“停止”时),我想将文本块“ BtnSTatus”的文本更改为“您已单击开始按钮”,然后返回到“单击开始”按钮”,则按钮标签再次显示文本“开始”。

Any suggestions how to fix these two issues? 有什么建议如何解决这两个问题?

My View: 我的观点:

<Button  Name="btnStartStop" Content="{Binding StartStopLabel}"  Command="{Binding ClickCommand}"  />
 <TextBlock Name="BtnStatus" Content="{Binding ButtonStatus}">

View.Cs code: View.Cs代码:

    private string _startStopLabel;
    public string StartStopLabel
    {
        get
        {
            return _startStopLabel;
        }
        set
        {                
            _startStopLabel =  value;                
            RaisePropertyChanged("StartStopLabel");
        }
    } 

    private string _ButtonStatus;
    public string ButtonStatus
    {
        get
        {
            return _ButtonStatus;
        }
        set
        {                
            _ButtonStatus =  value;                
            RaisePropertyChanged("ButtonStatus");
        }
    } 

ClickCommand event which is part of ICommand implementation in View.cs: ClickCommand事件是View.cs中ICommand实现的一部分:

  public System.Windows.Input.ICommand ClickCommand
    {
        get
        {
            return new DelegateCommand((o) =>
            {
                this.StartStopLabel = "Stop";
                Task.Factory.StartNew(() =>
                {
                    //call service on a background thread here...

                });
            });
        }
    }

Your problem is in the 您的问题出在

public System.Windows.Input.ICommand ClickCommand
{
    get
    {
        return new DelegateCommand(....

basically every time that property gets evaluated you will have a new command being generated. 基本上,每次评估该属性时,都会生成一个新命令。 So your command that you are bound to won't be the same one that you are changing the state of. 因此,您要绑定的命令将与您要更改其状态的命令不同。

Change your implementation to create the command in advance and return the same one. 更改您的实现以提前创建命令并返回相同的命令。

private System.Windows.Input.ICommand _clickCommand = new DelegateCommand((o) =>
        {
            this.StartStopLabel = "Stop";
            Task.Factory.StartNew(() =>
            {
                //call service on a background thread here...

            });
        });
public System.Windows.Input.ICommand ClickCommand { get { return _clickCommand; }}

In addition you will commonly see the pattern of creating the _clickCommand as a Lazy<ICommand> so that it only gets created on first usage. 此外,您通常会看到将_clickCommand创建为Lazy<ICommand>的模式,以便仅在首次使用时创建它。

I'd propose to change the ClickCommand property so that it returns different commands for start and stop with different texts: 我建议更改ClickCommand属性,以便它以不同的文本返回不同的开始和停止命令:

  1. ClickCommand is initialized with Start command. ClickCommand使用“开始”命令初始化。
  2. User executes command. 用户执行命令。
  3. Actions for start are executed through ICommand.Execute. 通过ICommand.Execute执行启动操作。
  4. ClickCommand is changed to return Stop command. 将ClickCommand更改为返回Stop命令。 OnPropertyChanged is raised for ClickCommand so that the UI binds to the new command. 为ClickCommand引发OnPropertyChanged,以便UI绑定到新命令。
  5. User executes command. 用户执行命令。
  6. Actions for stop are executed through ICommand.Execute. 停止操作通过ICommand.Execute执行。
  7. ClickCommand is changed to return Start command. ClickCommand更改为返回启动命令。 OnPropertyChanged is raised for ClickCommand so that the UI binds to the new command. 为ClickCommand引发OnPropertyChanged,以便UI绑定到新命令。 ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM