单击按钮时,为什么我的更新按钮文字没有显示?

时间:2018-04-18 08:06:48

标签: c# .net wpf mvvm data-binding

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

以下是代码:

的Xaml:

<Window x:Class="Cron.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Cron"
    mc:Ignorable="d"
    Title="Cron" Height="450" Width="650" MinWidth="600">
<StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <Button Content="{Binding PlayButtonText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                Command="{Binding StartStopCommand}" 
                VerticalAlignment="Top" Width="75"/>
    </StackPanel>
</StackPanel>

C#: 这是ViewModel。我想在这里设置按钮内容。

using System.Windows.Input;

namespace Cron
{
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";
    }

}
}

这是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();
    }
    }
}

这是基础的BaseViewModel。

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();
    }

}
}

2 个答案:

答案 0 :(得分:1)

更改属性值&#39; PlayButtonText&#39;如下所示,而不是_playButtonText

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

答案 1 :(得分:0)

您没有在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");
    }

}