I have two ObservableCollection<T>
and its bonded with two different ListView
UI.
ObservableCollection<Department> deptList = new ObservableCollection<Department>();
ObservableCollection<Teacher> teacherList = new ObservableCollection<Teacher>();
User can add or remove items from the collection in the run time.
At some point, if either of the lists is empty, the app freezes and throws an unhandled exception {Windows.UI.Xaml.UnhandledExceptionEventArgs}
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
Exception Message
Message = "Layout cycle detected. Layout could not complete.\r\nLayout cycle detected. Layout could not complete."
_message = "A cycle occurred while laying out the GUI."
Here is the code block for ListView
UI -
<ListView Header="Department List"
x:Name="depertmentList"
ItemsSource="{x:Bind deptList}"
SelectionMode="Multiple"
SelectionChanged="depertmentList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Department">
<StackPanel Margin="1">
<TextBlock Text="{x:Bind Dept}" Margin="1"/>
<TextBlock Text="{x:Bind SemesterCount}" Margin="1"/>
<TextBlock Text = "{Binding Courses, Converter={StaticResource ListToStringConverter}}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView Header="Teacher List"
x:Name="teacherListView"
ItemsSource="{x:Bind teacherList}"
SelectionMode="Single"
SelectionChanged="teacherListView_SelectionChanged"
Margin="2">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Teacher">
<StackPanel>
<TextBlock Text="{x:Bind Name}"/>
<TextBlock Text="{x:Bind Designation}" Margin="1"/>
<TextBlock Text="{x:Bind Dept}" Margin="1"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C# code for events
private void depertmentList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (depertmentList.SelectedItem == null)
{
removeDept.IsEnabled = false;
teacherListView.ItemsSource = teacherList;
}
else
{
removeDept.IsEnabled = true;
ObservableCollection<Department> t = new ObservableCollection<Department>();
Department temp = (Department)depertmentList.SelectedItem;
teacherListView.ItemsSource = from teacher in teacherList
where teacher.Dept == temp.Dept
select teacher;
}
}
private void teacherListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (teacherListView.SelectedItem == null)
{
removeTeacher.IsEnabled = false;
}
else
{
removeTeacher.IsEnabled = true;
}
}
Point to be noted, Teacher
class derives from Department
class. And if I remove a Department, all the teacher associated with that department gets removed too.
To prevent crashing the app, I had to add a hardcoded garbage item to the list which the user can remove later in run time.
And again if one of the lists is empty, the app crashes. But if there is at least one item in both lists, nothing happens.
Now, my question is How can I resolve this issue without adding any type of garbage item? I want to prevent crashing even if the lists are empty or null.