【问题标题】:On a button's click event, execute event handler in another class在按钮的单击事件上,在另一个类中执行事件处理程序
【发布时间】:2015-01-15 05:06:51
【问题描述】:

单击按钮时,我希望它执行一个事件处理程序方法,该方法是除窗口类之外的另一个类。

我相信创建一个绑定到另一个类中的事件处理程序方法的 ObjectDataProvider 对象,然后将所述对象绑定到 Click 事件可以解决问题,但它没有。

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.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Data.SqlClient;

namespace LoginNS
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class LoginWindow : Window
{

    public LoginWindow()
    {
        InitializeComponent();

    }

}

public class SQLServerClass
{
    public void ConnectSQLServer(object sender, RoutedEventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection("Data Source=tcp:172.16.1.71;Initial Catalog=Sample;User ID=sa;Password=hbkrko");
            conn.Open();
            MessageBox.Show("success");
        }
        catch
        {
            MessageBox.Show("db error");
        }
    }
}

}

这是资源以及我如何使用它,这是不正确的,因为我收到一条错误消息:

<ObjectDataProvider x:Key="loginFunction" ObjectType="{x:Type local:SQLServerClass}" MethodName="ConnectSQLServer"/>

<Grid DataContext="{Binding Path=LoginNS}" Width="400" Height="200">
    <Button x:Name="LoginButton" Style="{StaticResource LoginButton}" Click="{Binding Source={StaticResource loginFunction}}"/>
</Grid>

立即运行时错误:

Additional information: 'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '24' and line position '75'.

【问题讨论】:

    标签: c# .net wpf button


    【解决方案1】:

    ObjectDataProvider 用于创建可用作绑定源的对象实例。在您的情况下,ConnectSQLServer 方法不返回任何可用于绑定的对象。

    您的方案的最佳选择是使用 RelayCommand。您可以在http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

    了解如何实现这一目标

    在您的情况下,使用 RelayCommand 您的 SQLServerClass 将是这样的

    public class SQLServerClass
    {
        public SQLServerClass()
        {
            LoginCommand = new RelayCommand<object>(LoginCommandExecute, LoginCommandCanExecute);
        }
        public void ConnectSQLServer(object sender, RoutedEventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection("Data Source=tcp:172.16.1.71;Initial Catalog=Sample;User ID=sa;Password=hbkrko");
                conn.Open();
                MessageBox.Show("success");
            }
            catch
            {
                MessageBox.Show("db error");
            }
        }
    
        public ICommand LoginCommand { get; set; }
    
        private void LoginCommandExecute(object arg)
        {
            ConnectSQLServer(this, new RoutedEventArgs());
        }
    
        private bool LoginCommandCanExecute(object arg)
        {
            return true;
        }
    }
    

    还有你的 XAML

    <Window.Resources>
        <ObjectDataProvider x:Key="loginFunction" ObjectType="{x:Type local:SQLServerClass}"/>
    </Window.Resources>
    <Grid>
    
    
        <Grid  Width="400" Height="200">
            <Button x:Name="LoginButton"  Command="{Binding Path=LoginCommand, Source={StaticResource loginFunction}}"/>
        </Grid>
    </Grid>
    

    请注意,您可以使用MvvmLight 库。它已经包含 RelayCommand 类的实现和其他适用于 WPF MVVM 应用程序的有用类。

    【讨论】:

      【解决方案2】:

      你为什么不能用这个:

      InitializeComponent();
      sqlServerInstance = new SQLServerClass();
      LoginButton.Click += MainConnectSQLServer()
      

      private void MainConnectSQLServer(object sender, RoutedEventArgs e)
      {
          sqlServerInstance.ConnectSQLServer(sender, e);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多