我有 WPF 列表框,其中包含员工姓名作为显示数据,国家组合框和城市组合框,当我从该列表中选择数据时,我想在这两个组合框中显示员工的国家和城市
<Window.Resources>
<!--get employee name and add it to list-->
<DataTemplate x:Key="EmpFullNameTemplate" >
<TextBlock Text="{Binding EmpFullName}" FontSize="14" Foreground="Black" FontWeight="Bold"
FontFamily="Arial"></TextBlock>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="66*" />
<RowDefinition Height="204*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="71*" />
<ColumnDefinition Width="132*" />
<ColumnDefinition Width="190*" />
</Grid.ColumnDefinitions>
<!-- Emp Country-->
<TextBlock Name="blkEmpContries" Text=" الـبــلــد" Height="20" Margin="12,12,15,34" />
<ComboBox Grid.Column="1" Name="cmbContries" Height="20" Margin="0,12,18,34"
SelectionChanged="cmbContries_SelectionChanged" />
<!-- Emp Country-->
<TextBlock Grid.Row="1" Name="blkEmpCities" Text=" الـمـحـافـظـة" Height="20" Margin="12,9,0,175" />
<ComboBox Grid.Column="1" Grid.Row="1" Name="cmbEmpCities" Height="20" Margin="6,9,18,175" />
<ListBox Grid.Column="2" Grid.RowSpan="2" HorizontalAlignment="Stretch" Margin="11,13,12,16" Name="lstEmployees"
VerticalAlignment="Stretch" ItemsSource="{Binding Tables[0]}" Background="Salmon"
ItemTemplate="{StaticResource EmpFullNameTemplate}" Foreground="Black"
ScrollViewer.VerticalScrollBarVisibility="Auto"
BorderBrush="#FFAD7F30" SelectionChanged="lstEmployees_SelectionChanged" />
</Grid>
Int32 cntryID, crntEmpId, cityId;
string ConStr = "Data Source=.;Initial Catalog=MenofiaHospital;User ID=mohamed;Password=ewies";
#region get countries info
// this is "" Stored Procedure
// SELECT CountryID, CountryName FROM dbo.CountriesTbl
private void contriList()
{
SqlConnection SqlCon = new SqlConnection(ConStr);
// get all countries data
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = SqlCon;
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.CommandText = "SelAllCoubtries";
SqlCon.Open();
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = SqlCmd;
DataSet ds = new DataSet();
dap.Fill(ds);
SqlCon.Close();
bindContries(ds);
}
private void bindContries(DataSet dsGovs)
{
List<ContrisClass> cntrisList = new List<ContrisClass>();
for (int i = 0; i < dsGovs.Tables[0].Rows.Count; i++)
{
cntrisList.Add(new ContrisClass
(Convert.ToInt32(dsGovs.Tables[0].Rows[i]["CountryID"]),
dsGovs.Tables[0].Rows[i]["CountryName"].ToString()));
}
dsGovs = null;
cmbContries.ItemsSource = cntrisList;
cmbContries.DisplayMemberPath = "CountryName";
cmbContries.SelectedValuePath = "CountryID";
}
#endregion
#region get cities name according to selected country ID
// This is "SelAllCities" StoredProcedure
// select CityID,CityName from CitiesTbl where CountryID = @CountryID order by CityName
private void citiesList(int CountryID)
{
SqlConnection SqlCon = new SqlConnection(ConStr);
// twons menu
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = SqlCon;
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.CommandText = "SelAllCities";
SqlCon.Open();
SqlParameter ParCities = new SqlParameter();
ParCities.Direction = ParameterDirection.Input;
ParCities.ParameterName = "@CountryID";
ParCities.SqlDbType = SqlDbType.Int;
ParCities.Value = CountryID;
SqlCmd.Parameters.Add(ParCities);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = SqlCmd;
DataSet ds = new DataSet();
dap.Fill(ds);
SqlCon.Close();
bindTowns(ds);
}
private void bindTowns(DataSet dsTowns)
{
List<CityClass> townList = new List<CityClass>();
for (int i = 0; i < dsTowns.Tables[0].Rows.Count; i++)
{
townList.Add(new CityClass
(Convert.ToInt32(dsTowns.Tables[0].Rows[i]["CityID"]),
dsTowns.Tables[0].Rows[i]["CityName"].ToString()));
}
dsTowns = null;
cmbEmpCities.ItemsSource = townList;
cmbEmpCities.DisplayMemberPath = "CityName";
cmbEmpCities.SelectedValuePath = "CityID";
}
private void cmbContries_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cntryID = Convert.ToInt32(this.cmbContries.SelectedValue);
citiesList(cntryID);
}
#endregion
#region get all employees
// this is "SelEmpsInfo" StoredProcedure from SQL Server
// SELECT EmpID, EmpFullName FROM dbo.EmployeesTble
private void bindEmployeesInfo()
{
SqlConnection SqlCon = new SqlConnection(ConStr);
// guests menu
SqlCommand SqlCmdAllEmpsInfo = new SqlCommand();
SqlCmdAllEmpsInfo.Connection = SqlCon;
SqlCmdAllEmpsInfo.CommandType = CommandType.StoredProcedure;
SqlCmdAllEmpsInfo.CommandText = "SelEmpsInfo";
SqlCon.Open();
SqlDataAdapter dap1 = new SqlDataAdapter();
dap1.SelectCommand = SqlCmdAllEmpsInfo;
DataSet ds1 = new DataSet();
dap1.Fill(ds1, "EmployeesTble");
SqlCon.Close();
lstEmployees.DataContext = ds1;
}
#endregion
private void Window_Loaded(object sender, RoutedEventArgs e)
{
contriList();
bindEmployeesInfo();
}
#region get Employee info according to selected value from listbox and bind it to comboboxes
// this is "SelAllEmpInfo" StoredProcedure
// SELECT dbo.EmployeesTble.CityID, dbo.CitiesTbl.CityName, dbo.CitiesTbl.CountryID,
// dbo.CountriesTbl.CountryName FROM dbo.CitiesTbl INNER JOIN dbo.CountriesTbl ON
// dbo.CitiesTbl.CountryID = dbo.CountriesTbl.CountryID INNER JOIN dbo.EmployeesTble
// ON dbo.CitiesTbl.CityID = dbo.EmployeesTble.CityID where EmployeesTble.EmpID = @EmpID
public ArrayList SelAllEmpInfo(Int32 EmpId)
{
SqlConnection SqlCon = new SqlConnection(ConStr);
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = SqlCon;
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.CommandText = "SelAllEmpInfo";
SqlCon.Open();
SqlParameter ParEmpId = new SqlParameter();
ParEmpId.Direction = ParameterDirection.Input;
ParEmpId.ParameterName = "@EmpID";
ParEmpId.SqlDbType = SqlDbType.BigInt;
ParEmpId.Value = EmpId;
SqlCmd.Parameters.Add(ParEmpId);
SqlDataReader Dr = SqlCmd.ExecuteReader();
ArrayList empList = new ArrayList();
if (Dr.HasRows)
{
while (Dr.Read())
{
empList.Add(Dr[0].ToString());
empList.Add(Dr[1].ToString());
empList.Add(Dr[2].ToString());
empList.Add(Dr[3].ToString());
}
}
SqlCon.Close();
return empList;
}
private void lstEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
DataRowView drv = lstEmployees.SelectedValue as DataRowView;
crntEmpId = Convert.ToInt32(drv["EmpID"]);
ArrayList empList = SelAllEmpInfo(crntEmpId);
cmbEmpCities.DisplayMemberPath = empList[1].ToString();
cmbEmpCities.Text = empList[1].ToString();
cmbEmpCities.SelectedValuePath = empList[0].ToString();
cityId = Convert.ToInt32(empList[0]);
cmbContries.DisplayMemberPath = empList[3].ToString();
cmbContries.Text = empList[3].ToString();
cmbContries.SelectedValuePath = empList[2].ToString();
contriList();
}
catch { }
}
#endregion
}
class ContrisClass
{
private int _ContryID = -1;
private string _ContryName = "";
public ContrisClass(int contryID, string contryName)
{
this._ContryID = contryID;
this._ContryName = contryName;
}
public int CountryID
{
get { return _ContryID; }
set { _ContryID = value; }
}
public string CountryName
{
get { return _ContryName; }
set { _ContryName = value; }
}
}
class CityClass
{
private int _CityID = -1;
private string _CityName = "";
public CityClass(int cityID, string CityName)
{
this._CityID = cityID;
this._CityName = CityName;
}
public int CityID
{
get { return _CityID; }
set { _CityID = value; }
}
public string CityName
{
get { return _CityName; }
set { _CityName = value; }
}
}
我的问题是,当我从列表框“员工姓名”中选择数据时,两个组合框中都没有出现任何内容,但在调试模式下,我看到从数组列表中检索到的数据并绑定到组合框。
请提供任何帮助,谢谢。