在vs中新建wpf类型的MyWPFTest项目名称
修改app.xaml
<Application x:Class="MyWPFTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyWPFTest"
StartupUri="LoginWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
新建LoginWindow.xaml
<Window x:Class="MyWPFTest.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFTest"
mc:Ignorable="d"
Title="用户登录" Height="350" Width="350"
WindowStyle="ToolWindow"
Background="Gray"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="平台登录" FontSize="18" FontWeight="Bold" Grid.ColumnSpan="2"
HorizontalAlignment="Center" VerticalAlignment="Center">
</Label>
<Label Grid.Row="1" Content="服务器地址:" HorizontalAlignment="Right">
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="addr" Width="120" Height="23" HorizontalAlignment="Left"></TextBox>
<Label Grid.Row="2" Grid.Column="0" Content="用户名:" HorizontalAlignment="Right"></Label>
<TextBox Grid.Row="2" Grid.Column="1" Name="user" Width="120" Height="23" HorizontalAlignment="Left"></TextBox>
<Label Grid.Row="3" Grid.Column="0" Content="密 码:" HorizontalAlignment="Right"></Label>
<PasswordBox Grid.Row="3" Grid.Column="1" Name="password" Width="120" Height="23" HorizontalAlignment="Left"></PasswordBox>
<WrapPanel Grid.Row="4" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<Button Content="登录" Name="login" Width="45" Height="23" Margin="0,0,10,0" Background="Orange"
HorizontalAlignment="Center" VerticalAlignment="Center" Click="login_Click">
</Button>
<Button Content="取消" Name="cancel" Width="45" Height="23" Margin="10,0,0,0" Background="Gold"
HorizontalAlignment="Center" VerticalAlignment="Center" Click="cancel_Click" >
</Button>
</WrapPanel>
</Grid>
</Window>
修改它的cs文件:
using MyWPFTest.bean;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
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;
namespace MyWPFTest
{
/// <summary>
/// LoginWindow.xaml 的交互逻辑
/// </summary>
public partial class LoginWindow : Window
{
/*
String srvAdd = this.addr.Text;
String userName = this.user.Text;
String pwd = this.password.Password;
*/
public LoginWindow()
{
InitializeComponent();
}
//登录事件处理
private void login_Click(object sender, RoutedEventArgs e)
{
//this.Hide();
// new MainWindow().Show();
try
{
String severAddr = this.addr.Text;
String userName = this.user.Text;
String password = this.password.Password;
SqlConnection con = MSSQLDAO.MSSQLDAO.getCon(severAddr, userName, password, "123");
if (con!= null){
User user = new User();
user.SeverAddr = severAddr;
user.UserName = userName;
user.Password= password;
user.Database = "123";
this.Hide();
//new MainWindow(con).Show();
new MainWindow(user).Show();
}
}
catch (Exception ex)
{
MessageBox.Show("登录失败!"+ex.ToString());
}
}
//取消事件处理
private void cancel_Click(object sender, RoutedEventArgs e)
{
//退出程序
this.Hide();
Application.Current.Shutdown();
}
}
}
新建model文件并新建User文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyWPFTest.bean
{
class User
{
String severAddr = "";
String userName = "";
String password = "";
String database = "";
public string SeverAddr
{
get
{
return severAddr;
}
set
{
severAddr = value;
}
}
public string UserName
{
get
{
return userName;
}
set
{
userName = value;
}
}
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
public string Database
{
get
{
return database;
}
set
{
database = value;
}
}
}
}
修改MainWindow.xaml:
<Window x:Class="MyWPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFTest"
mc:Ignorable="d"
Title="主窗口" Height="Auto" Width="Auto"
WindowStartupLocation="CenterScreen"
Background="SeaShell"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ToolBar Background="Green">
<Button Name="query" Content="查找" Width="50" Height="30" Click="query_Click" ></Button>
</ToolBar>
<DockPanel Grid.Row="1">
<TreeView Name="tree">
//方式一:
<TreeViewItem Header="第一级目录">
<TreeViewItem Header="第二级目录"></TreeViewItem>
<TreeViewItem Header="第二级目录"></TreeViewItem>
</TreeViewItem>
//方式二:sql动态生成 ,去掉方式一的语句
</TreeView>
</DockPanel>
</Grid>
</Window>
<Window x:Class="MyWPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFTest"
mc:Ignorable="d"
Title="主窗口" Height="Auto" Width="Auto"
WindowStartupLocation="CenterScreen"
Background="SeaShell"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ToolBar Background="Green">
<Button Name="query" Content="查找" Width="50" Height="30" Click="query_Click" ></Button>
</ToolBar>
<DockPanel Grid.Row="1">
<TreeView Name="tree">
<TreeViewItem Header="第一级目录">
<TreeViewItem Header="第二级目录"></TreeViewItem>
<TreeViewItem Header="第二级目录"></TreeViewItem>
</TreeViewItem>
</TreeView>
</DockPanel>
</Grid>
</Window>
修改它的cs文件:
using MyWPFTest.bean;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
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.Navigation;
using System.Windows.Shapes;
namespace MyWPFTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
SqlConnection con = null;
User user=null;
public MainWindow()
{
InitializeComponent();
}
public MainWindow(SqlConnection con)
{
this.con = con;
InitializeComponent();
}
//连接db
private DataSet getSql(String sql, String table)
{
SqlConnection con = MSSQLDAO.MSSQLDAO.getCon(this.user.SeverAddr, this.user.UserName, this.user.Password, this.user.Database);
DataSet ds = MSSQLDAO.MSSQLDAO.getDataSet(sql, table, con);
con.Close();
return ds;
}
//查询处理
private void query_Click(object sender, RoutedEventArgs e)
{
String sql = "select dm,mc from t1 order by dm";
String t_user = "t1";
this.tree.Items.Clear(); //清空数据
DataSet ds = getSql(sql,t_user);
//判断获取数据是否有效
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//循环获取数据库返回内容行
foreach (DataRow row in ds.Tables[0].Rows)
{
String dm = row["dm"].ToString();
String mc = row[1].ToString();
TreeViewItem node = new TreeViewItem(); //定义一个树节点
node.Name = "_" + dm;
node.Header = dm + "-" + mc;
//如果是第一级直接向根节点里面写入,否则查找上级、。
if (dm.Length == 2)
{
this.tree.Items.Add(node);
}
else
{
foreach (TreeViewItem item_leve1 in this.tree.Items)
{
//如果找到当前新节点的上级节点,则在上级节点的items插入当前新节点
if (item_leve1.Name.Substring(1).Equals(dm.Substring(0, 2)))
{
item_leve1.Items.Add(node);
}
}
}
}
}
}
}
}
==========================over============