我有一个按钮。每次用户单击该按钮时,消息都应显示在按钮"1"上。第二次用户单击按钮时,它应显示"2",依此类推。我该怎么做?
enter code here
我必须使用WPF。
如果您使用的是MVVM,那么在Viewmodel中创建一个属性并设置默认值1,还可以使用Icommand来处理Button的点击事件。在ViewModel中,当您单击按钮时,可以增加或更改属性的值。
在XAML中,将按钮的Content属性绑定到ViewModel的属性。
我认为这正是您所需要的!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
button1.Content = null;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (button1.Content==null)
{
button1.Content = 1;
}
else
{
button1.Content = (Int32.Parse(button1.Content.ToString()) + 1).ToString();
}
}
}
}