On a button's click, i want it to execute an event handler method that is another class apart from the window class.
I believe creating a ObjectDataProvider object which is binded to the event handler method in the other class, then binding said object to the Click event would do the trick, but it didn't.
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");
}
}
}
}
Here is the resource and how i'm using it which is incorrect because i get an error message:
<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>
Immediate run-time error:
Additional information: 'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '24' and line position '75'.