问题描述
学员表中的性别存储在另一张表里,学员表中的性别字段存储的是GUID类型的,是性别表中的GUID字段。XAML:<ComboBoxName="cbo_StuGender"SelectedValue="{BindingStuGenderId}"DisplayMemberPath="CategoryName"SelectedValuePath="CategoryId"Width="116"Grid.Row="3"Grid.Column="1"Margin="3"></ComboBox>
XAML.CS:privatevoidWindow_Loaded(objectsender,RoutedEventArgse){StuDALstudal=newStuDAL();cbo_StuGender.ItemsSource=studal.GetByCategory("学员性别");}
DAL:///<summary>///获取类别列表中的数据///</summary>///<paramname="category"></param>///<returns></returns>publicStudentInfo[]GetByCategory(stringcategory){DataTabletable=SqlHelper.ExecuteDataTable("selectId,NamefromStuCategorywhereCategory=@Category",newSqlParameter("@Category",category));StudentInfo[]SiItems=newStudentInfo[table.Rows.Count];for(inti=0;i<table.Rows.Count;i++){DataRowrow=table.Rows[i];StudentInfosi=newStudentInfo();si.CategoryId=(Guid)row["Id"];si.CategoryName=(string)row["Name"];SiItems[i]=si;}returnSiItems;}
SQLHelper:publicstaticDataTableExecuteDataTable(stringsql,paramsSqlParameter[]parameters){using(SqlConnectionconn=newSqlConnection(ConnectionString)){conn.Open();using(SqlCommandcmd=conn.CreateCommand()){cmd.CommandText=sql;cmd.Parameters.AddRange(parameters);DataSetdataset=newDataSet();SqlDataAdapteradapter=newSqlDataAdapter(cmd);adapter.Fill(dataset);returndataset.Tables[0];}}}
stuModel:///<summary>///类别列表ID///</summary>publicGuidCategoryId{get;set;}///<summary>///类别列表名称///</summary>publicstringCategoryName{get;set;}///<summary>///学员性别///</summary>privateGuidstugenderid;publicGuidStuGenderId{get;set;}
性别表:学员表:UI:现在的问题在于,在XAML中设置了ComboBoxName="cbo_StuGender"SelectedValue="{BindingStuGenderId}"DisplayMemberPath="CategoryName"SelectedValuePath="CategoryId",UI启动时,COMBOBOX默认值就没法设置了,每次打开都是什么都没选。combobox所有设置默认选中的属性我都试了,都不行。求各位老师指点。