从一个窗口导航到另一个窗口时未获取值
本文关键字:窗口 另一个 获取 导航 一个 | 更新日期: 2023-09-27 18:32:20
如果导航到另一个窗口进行更新,则所选行将重置为null。如果在同一主页中完成更新,则正在获取该值并可以更新。如果我为每个视图都有不同的视图模型,那么为了在具有不同视图模型的新窗口中显示数据,我怎么也可以这样做。.
这是我的员工 查看型号代码
namespace EmployeeRegistration
{
class EmployeeViewModel : WindowItem, INotifyPropertyChanged, IDataErrorInfo
{
#region Declarations
SqlConnection sqlConnection = null;
SqlCommand insertCommand = null;
SqlCommand selectCommand = null;
SqlCommand updateCommand = null;
SqlDataAdapter sqlAdapter = null;
DataTable employeeTable = null;
DataSet sqlDataSet = null;
EmployeeModel employee;
private ICommand newEmployeeCommand;
private ICommand searchEmployeeCommand;
private ICommand saveEmployeeCommand;
private ICommand getEmployeeCommand;
public ICommand bindControlValueCommand;
public ICommand selectEmployeeCommand;
public ICommand updateDetailsCommand;
private Window window;
#endregion
#region Constructors
public EmployeeViewModel()
{
employee = new EmployeeModel();
SaveEmployeeCommand = new RelayCommand(new Action(saveEmployeeDetail));
SearchEmployeeCommand = new RelayCommand(new Action(searchEmployee));
GetEmployeeCommand = new RelayCommand(new Action(getEmployee));
SelectEmployeeCommand = new RelayCommand(new Action(selectEmployeeDetails));
NewEmployeeCommand = new RelayCommand(new Action(newEmployeeWindow));
UpdateDetailsCommand = new RelayCommand(new Action(updateEmployeeDetails));
bindControlValueCommand = new RelayCommand(new Action(bindControlValues));
Id = WindowsId.MainWindow;
Function = ProcessMessage;
WindowsManager.Add(Id, Function);
}
#endregion
#region Properties
//public EmployeeModel _Employee;
//public EmployeeModel Employee
//{
// get
// {
// return _Employee;
// }
// set
// {
// _Employee = value;
// OnPropertyChanged("Employee");
// }
//}
public DataRowView selectedEmployee;
public DataRowView SelectedEmployee
{
get
{
return selectedEmployee;
}
set
{
selectedEmployee = value;
OnPropertyChanged("SelectedEmployee");
}
}
private DataTable employeeDatatable;
public DataTable EmployeeDatatable
{
get { return employeeDatatable; }
set
{
employeeDatatable = value;
OnPropertyChanged("EmployeeDatatable");
}
}
public int Emp_ID
{
get
{
return employee.Emp_ID;
}
set
{
employee.Emp_ID = value;
OnPropertyChanged("Emp_ID");
}
}
public string FirstName
{
get
{
return employee.FirstName;
}
set
{
employee.FirstName = value;
OnPropertyChanged("FirstName");
//this.DetailView = new HomeViewModel(value);
}
}
public string LastName
{
get
{
return employee.LastName;
}
set
{
employee.LastName = value;
OnPropertyChanged("LastName");
}
}
public DateTime Dob
{
get
{
return employee.Dob;
}
set
{
employee.Dob = value;
OnPropertyChanged("Dob");
OnPropertyChanged("Age");
}
}
public int Age
{
get
{
return employee.Age = DateTime.Today.Year - Dob.Year;
}
set
{
employee.Age = value;
OnPropertyChanged("Age");
}
}
public string Street1
{
get
{
return employee.Street1;
}
set
{
employee.Street1 = value;
OnPropertyChanged("Street1");
}
}
public string Street2
{
get
{
return employee.Street2;
}
set
{
employee.Street2 = value;
OnPropertyChanged("Street2");
}
}
public string City
{
get
{
return employee.City;
}
set
{
employee.City = value;
OnPropertyChanged("City");
}
}
public string State
{
get
{
return employee.State;
}
set
{
employee.State = value;
OnPropertyChanged("State");
}
}
public string ZipCode
{
get
{
return employee.ZipCode;
}
set
{
employee.ZipCode = value;
OnPropertyChanged("ZipCode");
}
}
public string PhoneNumber
{
get
{
return employee.PhoneNumber;
}
set
{
employee.PhoneNumber = value;
OnPropertyChanged("PhoneNumber");
}
}
public string MobileNumber
{
get
{
return employee.MobileNumber;
}
set
{
employee.MobileNumber = value;
OnPropertyChanged("MobileNumber");
}
}
public string Email
{
get
{
return employee.Email;
}
set
{
employee.Email = value;
OnPropertyChanged("Email");
}
}
public string Web
{
get
{
return employee.Web;
}
set
{
employee.Web = value;
OnPropertyChanged("Web");
}
}
#endregion
#region Commands
public ICommand UpdateDetailsCommand
{
get
{
return updateDetailsCommand;
}
set
{
updateDetailsCommand = value;
}
}
public ICommand SelectEmployeeCommand
{
get
{
return selectEmployeeCommand;
}
set
{
selectEmployeeCommand = value;
}
}
public ICommand SaveEmployeeCommand
{
get
{
return saveEmployeeCommand;
}
set
{
saveEmployeeCommand = value;
}
}
public ICommand SearchEmployeeCommand
{
get
{
return searchEmployeeCommand;
}
set
{
searchEmployeeCommand = value;
}
}
public ICommand GetEmployeeCommand
{
get
{
return getEmployeeCommand;
}
set
{
getEmployeeCommand = value;
}
}
public ICommand NewEmployeeCommand
{
get
{
return newEmployeeCommand;
}
set
{
newEmployeeCommand = value;
}
}
public ICommand BindControlValueCommand
{
get
{
return bindControlValueCommand;
}
set
{
bindControlValueCommand = value;
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region SubMethods
public void newEmployeeWindow()
{
WindowsManager.SendMessage(WindowsId.MainWindow, null, ActionId.OpenWindow);
}
private bool ProcessMessage(object args, ActionId action)
{
if (action == ActionId.OpenWindow)
{
if (window == null)
{
window = new UpdateEmployeeDetails();
}
window.Show();
return true;
}
if (action == ActionId.CloseWindow)
{
if (window == null)
return false;
window.Close();
window = null;
return true;
}
return false;
}
private void bindControlValues()
{
this.Emp_ID = Convert.ToInt32(SelectedEmployee.Row.ItemArray[0]);
try
{
sqlConnection = new SqlConnection(Connection.connectionstring);
sqlConnection.Open();
selectCommand = new SqlCommand(AppConstants.StoredProcedures.GetDataProcedure, sqlConnection);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.Add(AppConstants.Parameters.Emp_ID, SqlDbType.Int).Value = this.Emp_ID; ;
sqlAdapter = new SqlDataAdapter(selectCommand);
sqlDataSet = new DataSet();
sqlAdapter.Fill(sqlDataSet);
this.FirstName = sqlDataSet.Tables[0].Rows[0][1].ToString();
this.LastName = sqlDataSet.Tables[0].Rows[0][2].ToString();
this.Dob = Convert.ToDateTime(sqlDataSet.Tables[0].Rows[0][3].ToString());
this.Age = Convert.ToInt32(sqlDataSet.Tables[0].Rows[0][4].ToString());
this.Street1 = sqlDataSet.Tables[0].Rows[0][5].ToString();
this.Street2 = sqlDataSet.Tables[0].Rows[0][6].ToString();
this.City = sqlDataSet.Tables[0].Rows[0][7].ToString();
this.State = sqlDataSet.Tables[0].Rows[0][8].ToString();
this.ZipCode = sqlDataSet.Tables[0].Rows[0][9].ToString();
this.PhoneNumber = sqlDataSet.Tables[0].Rows[0][10].ToString();
this.MobileNumber = sqlDataSet.Tables[0].Rows[0][11].ToString();
this.Email = sqlDataSet.Tables[0].Rows[0][12].ToString();
this.Web = sqlDataSet.Tables[0].Rows[0][13].ToString();
}
}
private void selectEmployeeDetails()
{
WindowsManager.SendMessage(WindowsId.MainWindow, null, ActionId.OpenWindow);
}
public void updateEmployeeDetails()
{
this.Emp_ID = Convert.ToInt32(SelectedEmployee.Row.ItemArray[0]);
try
{
SqlConnection sqlConnection = new SqlConnection(Connection.connectionstring);
sqlConnection.Open();
updateCommand = new SqlCommand(AppConstants.StoredProcedures.UpdateProcedure, sqlConnection);
updateCommand.CommandType = CommandType.StoredProcedure;
updateCommand.Parameters.Add(AppConstants.Parameters.Emp_ID, SqlDbType.Int).Value = this.Emp_ID;
updateCommand.Parameters.Add(AppConstants.Parameters.FirstName, SqlDbType.VarChar).Value = this.FirstName;
updateCommand.Parameters.Add(AppConstants.Parameters.LastName, SqlDbType.VarChar).Value = this.LastName;
updateCommand.Parameters.Add(AppConstants.Parameters.Dob, SqlDbType.VarChar).Value = Convert.ToDateTime(this.Dob);
updateCommand.Parameters.Add(AppConstants.Parameters.Age, SqlDbType.Int).Value = Convert.ToInt32(this.Age);
updateCommand.Parameters.Add(AppConstants.Parameters.Street1, SqlDbType.VarChar).Value = this.Street1;
updateCommand.Parameters.Add(AppConstants.Parameters.Street2, SqlDbType.VarChar).Value = this.Street2;
updateCommand.Parameters.Add(AppConstants.Parameters.City, SqlDbType.VarChar).Value = this.City;
updateCommand.Parameters.Add(AppConstants.Parameters.State, SqlDbType.VarChar).Value = this.State;
updateCommand.Parameters.Add(AppConstants.Parameters.ZipCode, SqlDbType.BigInt).Value = Convert.ToInt64(this.ZipCode);
updateCommand.Parameters.Add(AppConstants.Parameters.PhoneNumber, SqlDbType.BigInt).Value = Convert.ToInt64(this.PhoneNumber);
updateCommand.Parameters.Add(AppConstants.Parameters.MobileNumber, SqlDbType.BigInt).Value = Convert.ToInt64(this.MobileNumber);
updateCommand.Parameters.Add(AppConstants.Parameters.Email, SqlDbType.VarChar).Value = this.Email;
updateCommand.Parameters.Add(AppConstants.Parameters.Web, SqlDbType.VarChar).Value = this.Web;
int count = updateCommand.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("You Have Updated Successfully");
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
sqlConnection.Close();
updateCommand.Dispose();
}
}
public void saveEmployeeDetail()
{
int count1 = 0;
try
{
sqlConnection = new SqlConnection(Connection.connectionstring);
sqlConnection.Open();
insertCommand = new SqlCommand(AppConstants.StoredProcedures.InsertProcedure, sqlConnection);
insertCommand.CommandType = CommandType.StoredProcedure;
insertCommand.Parameters.Add(AppConstants.Parameters.FirstName, SqlDbType.VarChar).Value = this.FirstName;
insertCommand.Parameters.Add(AppConstants.Parameters.LastName, SqlDbType.VarChar).Value = this.LastName;
insertCommand.Parameters.Add(AppConstants.Parameters.Dob, SqlDbType.DateTime).Value = Convert.ToDateTime(this.Dob);
insertCommand.Parameters.Add(AppConstants.Parameters.Age, SqlDbType.Int).Value = Convert.ToInt32(this.Age);
insertCommand.Parameters.Add(AppConstants.Parameters.Street1, SqlDbType.VarChar).Value = this.Street1;
insertCommand.Parameters.Add(AppConstants.Parameters.Street2, SqlDbType.VarChar).Value = this.Street2;
insertCommand.Parameters.Add(AppConstants.Parameters.City, SqlDbType.VarChar).Value = this.City;
insertCommand.Parameters.Add(AppConstants.Parameters.State, SqlDbType.VarChar).Value = this.State;
insertCommand.Parameters.Add(AppConstants.Parameters.ZipCode, SqlDbType.BigInt).Value = Convert.ToInt64(this.ZipCode);
insertCommand.Parameters.Add(AppConstants.Parameters.PhoneNumber, SqlDbType.BigInt).Value = Convert.ToInt64(this.PhoneNumber);
insertCommand.Parameters.Add(AppConstants.Parameters.MobileNumber, SqlDbType.BigInt).Value = Convert.ToInt64(this.MobileNumber);
insertCommand.Parameters.Add(AppConstants.Parameters.Email, SqlDbType.VarChar).Value = this.Email;
insertCommand.Parameters.Add(AppConstants.Parameters.Web, SqlDbType.VarChar).Value = this.Web;
count1 = insertCommand.ExecuteNonQuery();
if (count1 > 0)
{
MessageBox.Show(AppConstants.Messages.SuccessMessages.InsertSuccess);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConnection.Close();
insertCommand.Dispose();
}
}
public void getEmployee()
{
try
{
sqlConnection = new SqlConnection(Connection.connectionstring);
sqlConnection.Open();
selectCommand = new SqlCommand(AppConstants.StoredProcedures.SelectProcedure, sqlConnection);
selectCommand.CommandType = CommandType.StoredProcedure;
sqlAdapter = new SqlDataAdapter(selectCommand);
employeeTable = new DataTable();
sqlAdapter.Fill(employeeTable);
EmployeeDatatable = null;
EmployeeDatatable = employeeTable;
}
}
public void searchEmployee()
{
//employee.Emp_ID = SelectedEmployee.Row["Emp_ID"].ToString();
sqlConnection = new SqlConnection(Connection.connectionstring);
sqlConnection.Open();
//Searching the Employee using Stored Procedure by passing the Firstname as parameter
selectCommand = new SqlCommand(AppConstants.StoredProcedures.SearchProcedure, sqlConnection);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.Add(AppConstants.Parameters.FirstName, SqlDbType.VarChar).Value = employee.FirstName;
selectCommand.ExecuteNonQuery();
sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = selectCommand;
employeeTable = new DataTable();
sqlAdapter.Fill(employeeTable);
EmployeeDatatable = null;
EmployeeDatatable = employeeTable;
}
#endregion
}
}
这是我的 XAML 主页
<Window x:Class="EmployeeRegistration.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:View="clr-namespace:EmployeeRegistration"
Title="Home" Height="595" Width="1096">
<Window.DataContext>
<View:EmployeeViewModel></View:EmployeeViewModel>
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding GetEmployeeCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Window.Resources>
<Style TargetType="Control" x:Key="myErrorTemplate">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1" CornerRadius="2.75" Grid.Column="0">
<AdornedElementPlaceholder Name="MyControl" Grid.Column="0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource myErrorTemplate}" />
</Window.Resources>
<Grid Height="984" Width="1089">
<Grid.RowDefinitions>
<RowDefinition Height="65"></RowDefinition>
<RowDefinition Height="36"></RowDefinition>
<RowDefinition Height="35" />
<RowDefinition Height="44"></RowDefinition>
<RowDefinition Height="45" />
<RowDefinition Height="49" />
<RowDefinition Height="56" />
<RowDefinition Height="43" />
<RowDefinition Height="40" />
<RowDefinition Height="37"></RowDefinition>
<RowDefinition Height="39*" />
<RowDefinition Height="42*" />
<RowDefinition Height="453*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="538"></ColumnDefinition>
<ColumnDefinition Width="121" />
<ColumnDefinition Width="149" />
<ColumnDefinition Width="110" />
<ColumnDefinition Width="182" />
</Grid.ColumnDefinitions>
</Grid>
</Window>
这是我的更新窗口 XAML
<Window x:Class="EmployeeRegistration.UpdateEmployeeDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:View="clr-namespace:EmployeeRegistration"
Title="UpdateEmployeeDetails" Height="575" Width="614">
<Window.DataContext>
<View:EmployeeViewModel></View:EmployeeViewModel>
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding BindControlValueCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
尝试将 ProcessMessage-Command 中的DataContext
传递给新窗口:
private bool ProcessMessage(object args, ActionId action)
{
if (action == ActionId.OpenWindow)
{
if (window == null)
{
window = new UpdateEmployeeDetails();
window.DataContext = this;
}
window.Show();
return true;
}
if (action == ActionId.CloseWindow)
{
if (window == null)
return false;
window.Close();
window = null;
return true;
}
return false;
}