刪除toolkit:listpicker綁定的最後一個元素會拋出SelectedItem must always be set to a valid value這個異常。
目測是這段代碼的問題:
01
private static void OnSelectedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
02
{
03
((ListPicker)o).OnSelectedItemChanged(e.OldValue, e.NewValue);
04
}
05
06
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "SelectedItem", Justification = "Property name.")]
07
private void OnSelectedItemChanged(object oldValue, object newValue)
08
{
09
if (newValue != null && (null == Items || Items.Count == 0))
10
{
11
if (null == Template)
12
{
13
// Can't set the value now; remember it for later
14
_deferredSelectedItem = newValue;
15
return;
16
}
17
else
18
{
19
throw new InvalidOperationException(Properties.Resources.InvalidSelectedItem);
20
}
21
}
22
23
// Validate new value
24
int newValueIndex = (null != newValue) ? Items.IndexOf(newValue) : -1;
25
26
if ((-1 == newValueIndex) && (0 < Items.Count))
27
{
28
throw new InvalidOperationException(Properties.Resources.InvalidSelectedItem);
29
}
30
31
// Synchronize SelectedIndex property
32
if (!_updatingSelection)
33
{
34
_updatingSelection = true;
35
SelectedIndex = newValueIndex;
36
_updatingSelection = false;
37
}
38
39
// Switch to Normal mode or size for current item
40
if (ListPickerMode.Normal != ListPickerMode)
41
{
42
ListPickerMode = ListPickerMode.Normal;
43
}
44
else
45
{
46
SizeForAppropriateView(false);
47
}
48
49
// Fire SelectionChanged event
50
var handler = SelectionChanged;
51
if (null != handler)
52
{
53
IList removedItems = (null == oldValue) ? new object[0] : new object[] { oldValue };
54
IList addedItems = (null == newValue) ? new object[0] : new object[] { newValue };
55
handler(this, new SelectionChangedEventArgs(removedItems, addedItems));
56
}
57
}
有興趣的動手改一改