【发布时间】:2016-10-30 22:30:23
【问题描述】:
我知道这是一个非常常见的问题,但是在更改 buttonContent 的“默认”时,我无法让按钮更新为“Pressed1”和“Pressed2”内容。看了几个问题,我找不到适合我的答案,我根本找不到这里有什么问题,所以这里是糟糕的代码:
带有按钮的窗口
public partial class MainWindow : Window
{
Code_Behind cB;
public MainWindow()
{
cB = new Code_Behind();
this.DataContext = cB;
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
cB.buttonPressed();
}
}
这是单独的类
public class Code_Behind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _buttonContent = "Default";
public string buttonContent
{
get { return _buttonContent; }
set {
if (_buttonContent != value)
{
buttonContent = value;
OnPropertyChanged("buttonContent");
}
}
}
public void buttonPressed()
{
int timesPressed = 0;
if (timesPressed != 1)
{
_buttonContent = "Pressed1";
timesPressed++;
}
else if (timesPressed != 2)
{
_buttonContent = "Pressed2";
timesPressed++;
timesPressed = 0;
}
}
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
【问题讨论】:
标签: c# wpf data-binding wpf-controls