Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我单击按钮时更新的按钮文本不显示?_C#_.net_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# 为什么我单击按钮时更新的按钮文本不显示?

C# 为什么我单击按钮时更新的按钮文本不显示?,c#,.net,wpf,mvvm,data-binding,C#,.net,Wpf,Mvvm,Data Binding,我正在开发一个小的WPF应用程序。我想在单击按钮时将其内容从“播放”更改为“停止”。我的问题是UI没有改变,尽管我在调试时进入了命令。我做错了什么?谢谢你的帮助 代码如下: Xaml: 这是RelayCommand using System; using System.Windows.Input; namespace Cron { /// <summary> /// A basic command that runs an action. /// </summary>

我正在开发一个小的WPF应用程序。我想在单击按钮时将其内容从“播放”更改为“停止”。我的问题是UI没有改变,尽管我在调试时进入了命令。我做错了什么?谢谢你的帮助

代码如下:

Xaml:

这是RelayCommand

using System;
using System.Windows.Input;

namespace Cron
{
/// <summary>
/// A basic command that runs an action.
/// </summary>
public class RelayCommand : ICommand
{
    private Action _action;

    public event EventHandler CanExecuteChanged = (sender, e) => { };

    public bool CanExecute(object parameter) => true;

    public RelayCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }
    }
}

按如下所示更改属性值“PlayButtonText”,而不是“PlayButtonText”

private void StartStop()
    {
        IsPlaying = !IsPlaying;
        PlayButtonText = IsPlaying ? "Stop" : "Play";
        OnPropertyChanged("PlayButtonText")
    }

您尚未在PlayButtonText上引发属性更改

public class MasterWindowViewModel : BaseViewModel
{
    public bool IsPlaying { get; set; } = false;

    private string _playButtonText = "Play";
    public string PlayButtonText
    {
        get => _playButtonText;
        set => _playButtonText = value;
    }

    public ICommand StartStopCommand { get; set; }


    public MasterWindowViewModel()
    {
        StartStopCommand = new RelayCommand(() => StartStop());
    }

    private void StartStop()
    {
        IsPlaying = !IsPlaying;
        PlayButtonText = IsPlaying ? "Stop" : "Play";
        OnPropertyChanged("PlayButtonText");
    }

}

你确定设置了datacontext吗?输出窗口中有绑定失败吗?我在代码中设置了上下文,如上所述。没有例外。
using System.ComponentModel;

namespace Cron
{
/// <summary>
/// Bas ViewModel wit PropertyChangedEvents.
/// </summary>
public class BaseViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// The PropertyChangedEvent.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

    /// <summary>
    /// Call this to fire a <see cref="PropertyChanged"/> event
    /// </summary>
    /// <param name="name"></param>
    public void OnPropertyChanged(string name)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}
}
namespace Cron
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MasterWindowViewModel();
    }

}
}
private void StartStop()
    {
        IsPlaying = !IsPlaying;
        PlayButtonText = IsPlaying ? "Stop" : "Play";
        OnPropertyChanged("PlayButtonText")
    }
public class MasterWindowViewModel : BaseViewModel
{
    public bool IsPlaying { get; set; } = false;

    private string _playButtonText = "Play";
    public string PlayButtonText
    {
        get => _playButtonText;
        set => _playButtonText = value;
    }

    public ICommand StartStopCommand { get; set; }


    public MasterWindowViewModel()
    {
        StartStopCommand = new RelayCommand(() => StartStop());
    }

    private void StartStop()
    {
        IsPlaying = !IsPlaying;
        PlayButtonText = IsPlaying ? "Stop" : "Play";
        OnPropertyChanged("PlayButtonText");
    }

}