using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfBrowserApplication1
{
class Classtest
{
static public double FontSizestatic = 20;
static private Window tmpwin; //敲键盘时关联的窗口;
static public DataGrid Add_DataGrid(Canvas father, double y, double x, double Height)
{
DataGrid tmpDataGrid = new DataGrid();
tmpDataGrid.Margin = new Thickness(x, y, 0, 0);
tmpDataGrid.FontSize = FontSizestatic;
tmpDataGrid.Height = Height;
//tmpDataGrid.Width = 20;
tmpDataGrid.Visibility = System.Windows.Visibility.Visible;
father.Children.Add(tmpDataGrid);
return tmpDataGrid;
}
static public void selectshow(int showmode, double y, double x, double FontSize, int WinH, DataTable DT)
{
//string str=initstr;
double dgwiny = 0, dgwinx = 0;
FontSizestatic = FontSize;
Window readwin = new Window();
tmpwin = readwin;
Canvas tmpfather = new Canvas();
readwin.Content = tmpfather;
switch (showmode)
{
case 0:
readwin.AllowsTransparency = true; readwin.Background = Brushes.Transparent;
readwin.WindowStyle = WindowStyle.None; tmpfather.Background = Brushes.Transparent;
readwin.Top = y; readwin.Left = x;
dgwiny = 0; dgwinx = 0;
break;
case 1:
readwin.Top = y; readwin.Left = x;
dgwiny = y; dgwinx = x;
break;
default:
readwin.Top = y; readwin.Left = x;
dgwiny = y; dgwinx = y;
break;
}
//TextBox tmpbox = Add_TextBox_static(tmpfather, 0, 0, tsstr, initstr, lines, length);
DataGrid tmpdatagrid = Add_DataGrid(tmpfather, dgwiny, dgwinx, WinH * (FontSize * 1.5));
tmpdatagrid.BorderBrush = Brushes.Red;
tmpdatagrid.Background = Brushes.LightSkyBlue;
tmpdatagrid.KeyDown += Tmpdatagrid_KeyDown;
tmpdatagrid.ItemsSource = DT.DefaultView;
Button tmpButton = Add_Button_static(tmpfather, WinH * (FontSize * 1.5) + 20, (SystemParameters.FullPrimaryScreenWidth - x * 2) / 2 - 20, "关闭", "close");
tmpButton.Click += TmpButton_Click;
readwin.ShowDialog();
return;
}
private static void TmpButton_Click(object sender, RoutedEventArgs e)
{
tmpwin.DialogResult = true;
}
static public Button Add_Button_static(Canvas father, double y, double x, string name, string uid)
{
Button stButton = new Button();
stButton.Name = name;
stButton.Content = name;
stButton.Uid = uid;
stButton.FontSize = FontSizestatic;
stButton.Margin = new Thickness(x, y, 0, 0);
stButton.Visibility = System.Windows.Visibility.Visible;
father.Children.Add(stButton);
return stButton;
}
private static void Tmpdatagrid_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Escape)
tmpwin.DialogResult = false;
else if (e.Key == System.Windows.Input.Key.Q)
tmpwin.DialogResult = false;
}
}
}
//上面的是完整的直接使用,下面要自己改成本地数据库访问的。
public partial class Window1 : Window
{
....
DataTable dt = new DataTable();
SqlDataAdapter da;
string StrSql, connectionString;
StrSql="select * from biaotest";
connectionString=...//连接数据库的串
da = new SqlDataAdapter(StrSql, connectionString);
da.Fill(dt);
Classtest.selectshow(0, 128, 410, 20, 12, dt.Tables); //主要是提供了这个函数直接查看表内容,必须返回后才操作其他功能,必定有场景会用到还可以改造他用。
.....
}