Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c1a8909

Browse files
committed
Add asserts and make things more robust
1 parent b10e96a commit c1a8909

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/GitHub.UI/Behaviours/AddEmptyItemToList.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System.Windows;
1+
using GitHub.VisualStudio;
2+
using System;
3+
using System.Collections;
4+
using System.Diagnostics;
5+
using System.Globalization;
6+
using System.Windows;
27
using System.Windows.Controls;
38
using System.Windows.Data;
49
using System.Windows.Interactivity;
@@ -22,21 +27,40 @@ protected override void OnAttached()
2227
previousSelection = AssociatedObject.SelectedItem;
2328
if (defaultValue == null)
2429
{
25-
var binding = AssociatedObject.GetBindingExpression(ListBox.ItemsSourceProperty);
30+
var binding = GetBinding();
31+
Debug.Assert(binding != null, "Cannot find ItemsSource binding. Did you configure it in the target Listbox?");
2632
if (binding == null)
27-
{
28-
var m = BindingOperations.GetMultiBindingExpression(AssociatedObject, ListBox.ItemsSourceProperty);
29-
binding = (BindingExpression)m.BindingExpressions[0];
30-
}
33+
return;
3134
defaultValue = PropertyPathHelper.GetValue(binding.ResolvedSource, binding.ResolvedSourcePropertyName);
3235
}
3336
dynamic items = AssociatedObject.ItemsSource;
34-
items.Insert(0, defaultValue);
37+
var list = items as IList;
38+
Debug.Assert(list != null, "ItemsSource data source is not an IList, cannot change it.");
39+
if (list == null)
40+
return;
41+
try
42+
{
43+
items.Insert(0, defaultValue);
44+
}
45+
catch (Exception ex)
46+
{
47+
#if DEBUG
48+
throw ex;
49+
#else
50+
VsOutputLogger.WriteLine(String.Format(CultureInfo.CurrentCulture, "Could not add default empty item to the bound ItemsSource data source - the collection does not allow insertion (or the type does not match). {0}", ex));
51+
#endif
52+
}
3553
}
3654
else if (AssociatedObject.SelectedIndex == 0)
3755
{
3856
dynamic items = AssociatedObject.ItemsSource;
39-
items.RemoveAt(0);
57+
var list = items as IList;
58+
Debug.Assert(list != null, "ItemsSource data source is not an IList, cannot change it.");
59+
Debug.Assert(list.Count == 0, "ItemsSource data source is empty, something went wrong.");
60+
if (list == null || list.Count == 0)
61+
return;
62+
if (items[0] == defaultValue)
63+
items.RemoveAt(0);
4064
}
4165
}
4266
if (e.RemovedItems.Count > 0)
@@ -50,6 +74,18 @@ protected override void OnAttached()
5074
};
5175
}
5276

77+
BindingExpression GetBinding()
78+
{
79+
var binding = AssociatedObject.GetBindingExpression(ListBox.ItemsSourceProperty);
80+
if (binding == null)
81+
{
82+
var m = BindingOperations.GetMultiBindingExpression(AssociatedObject, ItemsControl.ItemsSourceProperty);
83+
if (m != null && m.BindingExpressions.Count > 0)
84+
binding = (BindingExpression) m.BindingExpressions[0];
85+
}
86+
return binding;
87+
}
88+
5389
static class PropertyPathHelper
5490
{
5591
public static object GetValue(object obj, string propertyPath)

0 commit comments

Comments
 (0)