I'm using 2013 Q2. I have a Treeview bound to a basic custom business object. My data source is of type BindingList<Company>. Each company has a BindingList of projects, and each Project has a BindingList of Phases:
I've omitted the code for the Phase class, but it is similar.
I bound the data object like this:
This mostly works as expected. Changes to my data objects at all levels are reflected in the tree. When I add a top level node (Company), it is also immediately reflected in the tree. However, when I add a child node (Project) to a company, the child node is not displayed unless I set the BindingSource to null and then back to my data source. I could do this as a workaround, but that would also require a lot of fiddling around to restore the state of all the nodes in the tree.
Is it possible for the binding to work as I would expect and display newly added child nodes in the tree?
ETA: Version number
class
Company : INotifyPropertyChanged
{
private
string
_code;
private
readonly
BindingList<Project> _projects;
public
Company(
string
companyCode):
this
(companyCode,
null
){}
public
Company(
string
companyCode, IList<Project> projects)
{
_code = companyCode;
_projects = projects ==
null
?
new
BindingList<Project>() :
new
BindingList<Project>(projects);
}
public
String Code
{
get
{
return
_code; }
set
{
_code = value;
OnPropertyChanged(
"Code"
);
}
}
public
BindingList<Project> Projects
{
get
{
return
_projects; }
}
public
event
PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected
void
OnPropertyChanged(
string
name)
{
var handler = PropertyChanged;
if
(handler !=
null
)
{
handler(
this
,
new
PropertyChangedEventArgs(name));
}
}
}
class
Project : INotifyPropertyChanged
{
private
string
_code;
private
readonly
BindingList<Phase> _phases;
public
Project(
string
projectCode) :
this
(projectCode,
null
) { }
public
Project(
string
projectCode, IList<Phase> phases)
{
_code = projectCode;
_phases = phases ==
null
?
new
BindingList<Phase>() :
new
BindingList<Phase>(phases);
}
public
String Code
{
get
{
return
_code; }
set
{
_code = value;
OnPropertyChanged(
"Code"
);
}
}
public
BindingList<Phase> Phases
{
get
{
return
_phases; }
}
public
event
PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected
void
OnPropertyChanged(
string
name)
{
var handler = PropertyChanged;
if
(handler !=
null
)
{
handler(
this
,
new
PropertyChangedEventArgs(name));
}
}
}
I've omitted the code for the Phase class, but it is similar.
I bound the data object like this:
_companiesTreeView.DataSource = _dataSource;
_companiesTreeView.DisplayMember =
"Code\\Code\\Code"
;
_companiesTreeView.ChildMember =
"Companies\\Projects\\Phases"
;
_companiesTreeView.ValueMember =
"Code\\Code\\Code"
;
This mostly works as expected. Changes to my data objects at all levels are reflected in the tree. When I add a top level node (Company), it is also immediately reflected in the tree. However, when I add a child node (Project) to a company, the child node is not displayed unless I set the BindingSource to null and then back to my data source. I could do this as a workaround, but that would also require a lot of fiddling around to restore the state of all the nodes in the tree.
Is it possible for the binding to work as I would expect and display newly added child nodes in the tree?
ETA: Version number