选择分区时,ComboBox类别的绑定不会更新 选择除法时,ProjectCategories属性会填充两个结果,但视图不会更新。
如果我将ProjectCategories作为参考发送到ProjectCategoriesGetByDivisionId(),那么绑定会更新。
我不想传递对模型和数据类的引用。如何在不更改模型和数据类的情况下进行绑定更新?
这是Divisions ComboBox,用于更改Categories ComboBox的绑定值。
<ComboBox x:Name="Divisions" ItemsSource="{Binding Divisions}" DisplayMemberPath="Name" SelectedValuePath="DivisionId">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding DivisionChanged}" CommandParameter="{Binding ElementName=Divisions, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
未更新的ComboBox
<ComboBox x:Name="Categories" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" />
触发Divisions SelectedChanged事件时触发的方法。
private void DivisionChanged(Division d)
{
ProjectCategories = ProjectCategory.GetByDivisionId(d.DivisionId);
}
ComboBox绑定到
的ViewModel属性public ObservableCollection<ProjectCategory> ProjectCategories
{
get { return projectCategories; }
set
{
projectCategories = value;
if (base.PropertyChangedHandler != null)
base.PropertyChangedHandler(this, new PropertyChangedEventArgs("ProjectCategories"));
}
}
名为
的模型方法public static ObservableCollection<ProjectCategory> GetByDivisionId(int divisionId)
{
return ProjectData.ProjectCategoriesGetByDivisionId(divisionId);
}
我认为其余部分是自我解释的。
public static ObservableCollection<ProjectCategory> ProjectCategoriesGetByDivisionId(int divisionId)
{
ObservableCollection<ProjectCategory> projectCategory = new ObservableCollection<ProjectCategory>();
SqlConnection conn = null;
try
{
conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("TRK_ProjectCategory_GetByDivisionId", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@DivisionId", SqlDbType.Int).Value = divisionId;
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
projectCategory.Add(ObjectConstructors.ProjectCategoryConstructor(sdr));
}
catch (Exception ex)
{
ErrorHandler.EmailLog("MineralsData", "public static ObservableCollection<ProjectCategory> ProjectCategoriesGetByDivisionId(int divisionId)", ex.ToString(), string.Empty);
throw ex;
}
finally
{
if (conn != null)
conn.Close();
conn = null;
}
return projectCategory;
}
public static ProjectCategory ProjectCategoryConstructor(SqlDataReader dr)
{
ProjectCategory ec = new ProjectCategory();
ec.CategoryId = dr["CategoryId"].SDR_GetInt();
ec.Name = dr["Name"].SDR_GetString();
ec.Description = dr["Description"].SDR_GetString();
ec.LastModified = dr["LastModified"].SDR_GetDateTime();
ec.ModifiedBy = dr["ModifiedBy"].SDR_GetString();
return ec;
}
感谢您的帮助。
答案 0 :(得分:1)
你的组合框绑定到一个名为“类别”的属性。您没有显示此属性的任何其他代码。只有在为类别调用propertychanged事件时才会更新。你的意思是绑定到“ProjectCategories”吗?
答案 1 :(得分:0)
正如@Lee所说,你的xmal绑定到一个名为Categories
的对象,但在viewmodel中你有ProjectCategories
,将类别组合框更改为
<ComboBox x:Name="Categories" ItemsSource="{Binding ProjectCategories}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" />
您不需要调用propertychanged事件,因为您已经在setter中提升PropertyChangedEventHandler
。