The Binding on the ComboBox Categories is not updating when a Division is selected.
When a division is selected the ProjectCategories Property does get populated with two results, but the view does not update.
If I send ProjectCategories as a ref through to ProjectCategoriesGetByDivisionId() then the binding does update.
I don’t want to pass references to my model and data classes. How can I have the binding update without changing my model and data classes?
Here is the Divisions ComboBox that changes the value of the binding for the 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>
The ComboBox that isn’t updating
<ComboBox x:Name="Categories" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" />
The method that gets fired when the Divisions SelectedChanged event is fired.
private void DivisionChanged(Division d)
{
ProjectCategories = ProjectCategory.GetByDivisionId(d.DivisionId);
}
ViewModel Property the ComboBox is binding to
public ObservableCollection<ProjectCategory> ProjectCategories
{
get { return projectCategories; }
set
{
projectCategories = value;
if (base.PropertyChangedHandler != null)
base.PropertyChangedHandler(this, new PropertyChangedEventArgs("ProjectCategories"));
}
}
Model Method that is called
public static ObservableCollection<ProjectCategory> GetByDivisionId(int divisionId)
{
return ProjectData.ProjectCategoriesGetByDivisionId(divisionId);
}
I think the rest is self explanitory.
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;
}
Thanks for any help.
Your combobox is binding to a property called “categories”. You don’t show any other code for this property. It will only update if you call propertychanged event for categories. Did you mean to bind to “ProjectCategories”?