在按钮的单击上,我希望它执行一个事件处理程序方法,该方法是除窗口类之外的另一个类。
我相信创建一个绑定到另一个类中的事件处理程序方法的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'.
答案 0 :(得分:2)
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应用程序的其他有用类。
答案 1 :(得分:0)
为什么你不能用这个:
InitializeComponent();
sqlServerInstance = new SQLServerClass();
LoginButton.Click += MainConnectSQLServer()
和
private void MainConnectSQLServer(object sender, RoutedEventArgs e)
{
sqlServerInstance.ConnectSQLServer(sender, e);
}