Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
使用WPF记录鼠标点击并显示在按钮上_Wpf - Fatal编程技术网

使用WPF记录鼠标点击并显示在按钮上

使用WPF记录鼠标点击并显示在按钮上,wpf,Wpf,我有一个钮扣。每次用户单击该按钮时,应在按钮“1”上显示消息,当用户第二次单击按钮时,应显示“2”,以此类推。 我该怎么做 enter code here 我必须用WPF来做 如果您使用的是MVVM,则在Viewmodel中创建一个属性,并将默认值设置为1,同时使用Icommand作为按钮的句柄单击事件。 在ViewModel中,单击按钮,然后增加或更改属性的值 在XAML中,将按钮的内容属性绑定到ViewModel的属性。我认为这正是您所需要的 using System; using

我有一个钮扣。每次用户单击该按钮时,应在按钮“1”上显示消息,当用户第二次单击按钮时,应显示“2”,以此类推。 我该怎么做

enter code here

我必须用WPF来做

如果您使用的是MVVM,则在Viewmodel中创建一个属性,并将默认值设置为1,同时使用Icommand作为按钮的句柄单击事件。 在ViewModel中,单击按钮,然后增加或更改属性的值


在XAML中,将按钮的内容属性绑定到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();
              }


          }
      }
  }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
button1.Content=null;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
if(button1.Content==null)
{
按钮1.内容=1;
}
其他的
{
button1.Content=(Int32.Parse(button1.Content.ToString())+1.ToString();
}
}
}
}

我收到一个错误,告诉我“输入字符串的格式不正确”您是否设置了button1.content=null??如果按钮上有一些初始文本,那么您应该首先将按钮上下文与初始文本进行比较,也就是说,您应该编写如下条件:If(button1.content.ToString()=“SomeText”)。。。代替与nullI进行比较,我得到了!!我不得不将按钮内容更改为“{Binding}”