简体   繁体   English

在 UserControl 按钮单击事件中从父 Window 调用公共 void 方法

[英]Calling public void method from Parent Window in UserControl button click event

I have a label in my MainWindow navigation menu, and the content is binding to SQL command and that method is called public void HBD_Count()我的 MainWindow 导航菜单中有一个label ,内容绑定到 SQL 命令,该方法称为public void HBD_Count()

I have various UserControls where on a Button_Click I would like to call public void HBD_Count() so that value in my label can be refreshed.我有各种UserControls ,在Button_Click上我想调用public void HBD_Count()以便可以刷新label中的值。

I tried called the public partial class from my MainWindow to my UserControl pages but it does not work.我尝试将public partial class从我的 MainWindow 调用到我的UserControl页面,但它不起作用。

Here is my code in my Main Window, which populates the label :这是我在主 Window 中的代码,它填充了label

    public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MyWindow_Loaded;
        HBD_Count();
    }


        public void HBD_Count()
    {
        try
        {
            SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

            string selectQuery = ("SELECT COUNT(*) AS HBDCount FROM hb_Disputes WHERE (ASSGNTO = 'E099255') AND (STATUS = 3)");
            connection.Open();
            SqlCommand command = new SqlCommand(selectQuery, connection);

            SqlDataReader sqlReader = command.ExecuteReader();

            while (sqlReader.Read())
            {
                HBD_Counts.Content = sqlReader["HBDCount"].ToString();
            }
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

Here is my code in my UserControl page:这是我的UserControl页面中的代码:

    public partial class Import_HighBill : UserControl
{
    public string ValueString { get; set; }
    public partial class MainWindow : Window { }

    public Import_HighBill()
    {
        InitializeComponent();
        AssignList();
    }
        private void butn_Assign_Click(object sender, RoutedEventArgs e)
    {
            SqlConnection con = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

            try
            {
                SqlCommand cmd = new SqlCommand("UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID=@DSP_ID", con);
                cmd.Parameters.AddWithValue("@DSP_ID", txt_ID.Text);
                // cmd.Parameters.AddWithValue("@DATERSLVD", DBNull.Value);


                //  Analyst Name
                if (cmb_AnalystName.SelectedValue == null)
                {
                    cmd.Parameters.AddWithValue("@ASSGNTO", DBNull.Value);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@ASSGNTO", cmb_AnalystName.SelectedValue);
                }

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



            // Here is where I am trying to call the method
               HBD_Count();




                // Clear Search Fields
                cmb_AnalystName.SelectedIndex = -1;
                MessageBox.Show("Dispute Assinged!!!");
                AssignList();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

    }

You can use the following code to accomplish this task:您可以使用以下代码来完成此任务:

Window parentWindow = Window.GetWindow(this);
((MainWindow)parentWindow).HBD_Count();

The MVVM concept allows simplifying the task if you need to call different various methods.如果您需要调用不同的各种方法,MVVM 概念可以简化任务。 In this case you will need to include the method call inside the Command and will be able to access it using a RelativeSource binding .在这种情况下,您需要在Command中包含方法调用,并且能够使用RelativeSource 绑定来访问它。

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

相关问题 用户控件中的 wpf 单击按钮调用父窗口中的事件 - wpf click button in usercontrol calls event in parent window 从按钮单击方法调用私有 void function - Calling a private void function from a button click method 在Silverlight中从子UserControl调用父UserControl方法 - Calling parent UserControl method from a child UserControl in Silverlight 单击()时的 Unity 按钮。 public void Method() vs public void Method(arr1[], arr2[], arr3[]) - Unity Button On Click(). public void Method() vs public void Method(arr1[], arr2[], arr3[]) 用户控件点击事件调用新的用户控件 - Usercontrol click event calling new usercontrol 在按钮单击事件中从另一个类Person调用方法PresentPerson() - calling on a method PresentPerson() from another class Person in a button click event 使用功能键(如F3,F6)从其父窗体调用UserControl的按钮单击事件 - Invoking a Button Click Event of UserControl from its parent Form using Function Keys Like (F3,F6) 我可以在UserControl的UpdatePanel中调用父级的事件方法吗? - Can I call an event method of a parent from within an UpdatePanel on a UserControl? 从MainWindow在UserControl中调用事件 - Calling event in UserControl from MainWindow 从另一个 UserControl 调用 UserControl 中的方法 - Calling a method in a UserControl from another UserControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM