【发布时间】:2016-03-13 07:26:12
【问题描述】:
我有组合框,当单击按钮时,该组合框正确填充了一些字段 ID。
private void Button_Click(object sender, RoutedEventArgs e)
{
results.Items.Add(ID);
}
现在我想当我更改一些值以删除以前的值(或者如果我在组合框中有多个值的情况下的值),但我总是遇到异常(如果已经在组合框中选择了一些值) 我试图在顶部添加该方法:
results.Items.Clear();
我试过这个:
for (int i = 0; i < results.Items.Count; i++)
{
results.Items.RemoveAt(i);
i--;
}
但总是出现异常:
System.ArgumentException:值不在预期范围内。 在 MS.Internal.XcpImports.MethodEx(IntPtr ptr,字符串名称,CValue[] cvData) 在 MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData) 在 MS.Internal.XcpImports.Collection_Add[T](PresentationFrameworkCollection'1 集合,对象值) 在 System.Windows.PresentationFrameworkCollection'1.AddImpl(对象值) 在 System.Windows.Controls.ItemCollection.AddImpl(对象值) 在 System.Windows.Controls.ItemCollection.AddInternal(对象值) 在 System.Windows.PresentationFrameworkCollection'1.Add(T 值) 在 SXPCreateIncident3.SilverlightControl1.results_SelectionChanged(对象发送者,SelectionChangedEventArgs e) 在 System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArg
如果我没有使用 Clear (Remove) 清除此部分,那么组合框在每个按钮 Click 上都有更多元素,但我需要在单击按钮时清除以前的内容。
【问题讨论】:
-
注意:您不应该在 for 循环中修改迭代器计数器(除非您知道自己在做什么)...
-
嗨,好的,但是如果我已经在 Combobox 中选择了某些项目,为什么 Clear 方法不起作用?
标签: c# visual-studio silverlight combobox silverlight-5.0