简体   繁体   English

我如何从另一个类调用按钮单击方法

[英]how can i call a button click method from another class

hello everyone i'm new to c# and wpf programming and i'm trying to create a dynamic menu where i have + and - buttons which affect a text box which represents quantity. 大家好,我是C#和WPF编程的新手,我正在尝试创建一个动态菜单,其中有+和-按钮,这些按钮会影响代表数量的文本框。 so in a grid i call a class called productcard which i call in a page to fill the grid with the products. 因此,在网格中,我调用了一个名为productcard的类,该类在页面中调用以用产品填充网格。 now the problem is how can i use the click event inside of the product card class in my page where i have multiple cards. 现在的问题是,在我有多张卡片的页面中,如何使用产品卡片类中的click事件。

class productcard 
{

     Button plus = new Button();
    Button minus= new Button();
    public TextBox qtyl = new TextBox();
    Grid z = new Grid();
    public int left;
    public int top;

    GroupBox yy;
    public GroupBox XX { get { return this.yy; } set { this.yy = value; } }


    public productcard(int left , int top )
    {
        this.left = left;
        this.top = top;
        Thickness margin = new Thickness(left, top, 0, 0);
        Thickness bmar = new Thickness(0, 0, 0, 0);
        plus.Height = 30;
        plus.Width = 40;
        plus.VerticalAlignment = VerticalAlignment.Bottom;
        plus.HorizontalAlignment = HorizontalAlignment.Right;
        plus.Content = "+";
        plus.HorizontalContentAlignment = HorizontalAlignment.Center;

        // - button 
        minus.Height = 30;
        minus.Width = 40;
        minus.VerticalAlignment = VerticalAlignment.Bottom;
        minus.HorizontalAlignment = HorizontalAlignment.Left;
        minus.Content = "-";
        minus.HorizontalContentAlignment = HorizontalAlignment.Center;
        // add the button to the grid 
        z.Children.Add(plus);
        z.Children.Add(minus);
        // creat text box 
        qtyl = new TextBox();
        qtyl.Height = 30;
        qtyl.Width = 30;
        qtyl.Background = Brushes.White;
        qtyl.VerticalAlignment = VerticalAlignment.Bottom;
        qtyl.HorizontalAlignment = HorizontalAlignment.Center;
        qtyl.Text = "0";
        // add text box to the grid inside the group box
        z.Children.Add(qtyl);
        // creat group box
        GroupBox yy = new GroupBox();
        yy.Margin = margin;
        yy.VerticalAlignment = VerticalAlignment.Top;
        yy.HorizontalAlignment = HorizontalAlignment.Left;
        yy.Content = z;
        yy.Height = 150;
        yy.Width = 150;
        XX = yy;
        // insert group box in the produc grid 
    }




   public void plus_Click(object sender, EventArgs e)
    {
       // this.plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        MessageBox.Show(" + has been cliked");
        int result=Convert.ToInt32(qtyl.Text)+1;
        qtyl.Text = result.ToString();
    }

    private void minus_Click(object sender, EventArgs e)
    {
        int result = Convert.ToInt32(qtyl.Text) - 1;
        qtyl.Text = result.ToString();
    }

}

You can make a handler for your button like this: 您可以像这样为按钮创建处理程序:

Button myButton=new Button(); 

myButton.Click += delegate(object sender, RoutedEventArgs e) {
   //handle event
};

I hope this helps. 我希望这有帮助。

Reza is correct in how to write more code for a Button generated in code. Reza在如何为代码中生成的Button编写更多代码方面是正确的。

However, I would give you a word of warning that this is not proper WPF usage of MVVM and you might be putting yourself down a path for trouble later. 但是,我要警告您,这不是MVVM的WPF正确使用方式,以后您可能会陷入麻烦。

I would suggest having your view's buttons bind to an ICommand that can be defined in a ViewModel that will handle doing the logic for your text update. 我建议您将视图的按钮绑定到可以在ViewModel中定义的ICommand,该ICommand将处理文本更新的逻辑。 As you mentioned, you're having different view controls represent the data based on your button press. 如前所述,您具有不同的视图控件,这些控件根据按钮的按下来表示数据。 You're currently surviving as the view's are directly updating each other (THIS IS BAD). 您目前还活着,因为视图之间是直接相互更新的(这很糟糕)。 The moment you want to represent this data in other views, say you want your button to update 5 labels in 3 different layouts in 2 windows, you're going to have unmaintainable references in your views. 当您想在其他视图中表示此数据时,比如说您希望按钮在2个窗口中以3种不同的布局更新5个标签,那么您的视图中将具有无法维护的引用。

If you have the ViewModel get a command from your view bound to the button, you can have the command logic update the property in the ViewModel that multiple views can be bound to and update them all at the same time via INotifyPropertyChanged. 如果您使ViewModel从您的视图中获取了绑定到按钮的命令,则可以让命令逻辑更新可以绑定到多个视图的ViewModel中的属性,并通过INotifyPropertyChanged同时更新所有视图。 Not to mention, ICommand can also let you disable buttons cleanly from being clicked. 更不用说,ICommand还可以让您完全禁止单击按钮。

Consider taking an hour to check out this tutorial to see the separation of View and ViewModel. 考虑花费一个小时来查看本教程,以了解View和ViewModel的分离。 What you're doing now looks like it's setting you up for a world of hurt later... 您现在正在做的事情看起来像是让您以后遭受了很多伤害...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM