WPF 按钮内容绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26055700/
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:34:10  来源:igfitidea点击:

WPF Button content binding

c#wpfxaml

提问by GETah

I am sure the solution to this problem is obvious but I simply can't figure it out. I tried every answer I could find on SO with no hope.

我确信这个问题的解决方案是显而易见的,但我就是想不通。我毫无希望地尝试了所有可以在 SO 上找到的答案。

Problem

问题

I have a simple WPF form that holds a button. The button's text is defined by a view model that changes the text content on click. For the sake of completeness, below is my xaml and the view model laying behind.

我有一个包含按钮的简单 WPF 表单。按钮的文本由视图模型定义,该模型在单击时更改文本内容。为了完整起见,下面是我的 xaml 和后面的视图模型。

XAML:

XAML:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vms="clr-namespace:MyVm"
        Height="163.762" Width="201.758" WindowStyle="ToolWindow" ResizeMode="NoResize" Topmost="True" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <vms:MyVm />
    </Window.DataContext>
    <Button Name="Action" Content="{Binding Path=NextAction, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Margin="0" MinHeight="50" FontWeight="Bold" FontSize="16" Click="Action_Click"></Button>
</Window>

ViewModel:

视图模型:

public class MyVm: INotifyPropertyChanged
{
    private String _nextAction = "START";
    public String NextAction {
        get { return _nextAction; }
        set
        {
            _nextAction = value;
            OnPropertyChanged("NextAction");
        }

    }

    public void ExecuteAction()
    {
        if (_nextAction == "START")
            NextAction = "STOP";

        if (_nextAction == "STOP")
            NextAction = "START";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Question

When I click on the button, I want the text to change from "START" to "STOP" and vice versa.

当我单击按钮时,我希望文本从“开始”更改为“停止”,反之亦然。

  • The logic in the view model seem to work fine.
  • The OnPropertyChangedis correctly called.
  • The handlerinside OnPropertyChangedis not null
  • 视图模型中的逻辑似乎工作正常。
  • OnPropertyChanged正确调用。
  • handler里面OnPropertyChanged是不是null

Clicking the button has no effect on the UI. What am I doing wrong?

单击该按钮对 UI 没有影响。我究竟做错了什么?

回答by BenjaminPaul

The following logic is incorrect...

以下逻辑不正确...

public void ExecuteAction()
    {
        if (_nextAction == "START")
            NextAction = "STOP";

        if (_nextAction == "STOP")
            NextAction = "START";
    }

It should be...

它应该是...

public void ExecuteAction()
    {
        if (_nextAction == "START") {
            NextAction = "STOP";
        } else {
            NextAction = "START";
        }
    }

You are changing the next action to stop then the following lines of code are changing it back again immediately.

您正在将下一个操作更改为停止,然后以下代码行立即将其更改回来。