I am currently upgrading the tool of my predecessor and I'm stuck on a TreeView issue. His software analyzed data one time and displayed the results in a TreeView. I changed the tool to analyze real-time data and hence the user gets real time results. Now, I would like the tree view nodes to keep expanded when the user expands it, even after an update. I know this topic has been discussed several times and I spent hours reading the answers but I never found a solution for my problem.
The basic problem is: there is no stable tree any more and currently the tree is just re-created every time. I have no idea about data binding or XAML up to now and few time to learn programming in XAML. Below you can see the code like it was created by my predecessor. I did not change anything up to now, I just recall this method every 2-5 seconds.
Is there an (easy?) way to store the information about expanded nodes anywhere (global?) and re-expand it after refresh or do I really need to get a deeper understanding of WPF first?
foreach (Analyzer.ALARM_GROUP alarmGroup in alarmGroupList)
{
//Display Problem Group
TreeViewItem groupItem = new TreeViewItem();
List<String> ListAreas = new List<string>();
string areas = "";
//GROUP HEADER
//display alarm group which contains more than one alarm
if (alarmGroup.AlarmList.Count > 1)
{
groupItem.Header = Convert.ToString(alarmGroup.AlarmList.Count) + " --- " + alarmGroup.AlarmList[0].EventTime + " " + alarmGroup.AlarmList[0].AlarmText + "\n "
+ alarmGroup.AlarmList[alarmGroup.AlarmList.Count - 1].EventTime + " " + alarmGroup.AlarmList[alarmGroup.AlarmList.Count - 1].AlarmText
+ " --- " + " AREAS: " + areas;
}
else //display alarm group with only one alarm message
{
groupItem.Header = Convert.ToString(alarmGroup.AlarmList.Count) + " --- " + alarmGroup.AlarmList[0].EventTime + " " + alarmGroup.AlarmList[0].AlarmText + " --- " + " AREA: " + areas;
}
//HEADLINE of single Alarm Item
TreeViewItem alarmItem = new TreeViewItem();
alarmItem.Header = "[EVENTTIME] [ALARMTAG] --- [OPCCONDITION] --- [SEVERITY] --- [AREA] --- [ALARMTEXT]";
alarmItem.FontWeight = FontWeights.SemiBold;
groupItem.Items.Add(alarmItem);
//Display single alarm messages
foreach (var alarm in alarmGroup.AlarmList)
{
alarmItem = new TreeViewItem();
alarmItem.Header = alarm.EventTime + " --- " + alarm.AlarmTag + " --- " + alarm.Condition + " --- " + alarm.Severity + " --- " + alarm.Area + " --- " + alarm.AlarmText;
alarmItem.FontWeight = FontWeights.Normal;
groupItem.Items.Add(alarmItem);
}
AlarmPresentationBox.Items.Add(groupItem);