Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么ListBox.Items.IsReadOnly=true?(F#/Silverlight)_Silverlight_F#_Listbox - Fatal编程技术网

为什么ListBox.Items.IsReadOnly=true?(F#/Silverlight)

为什么ListBox.Items.IsReadOnly=true?(F#/Silverlight),silverlight,f#,listbox,Silverlight,F#,Listbox,我试图从已创建的列表框中删除一些对象,出于某种原因,ListBox.Items.IsReadOnly为true 以下呼叫不起作用: myListBox.Items.Add("whatever") myListBox.Items.Add("stuff") myListBox.Items.Remove("whatever") 我得到一个例外: {System.InvalidOperationException: Operation not supported on read-only collec

我试图从已创建的列表框中删除一些对象,出于某种原因,ListBox.Items.IsReadOnly为true

以下呼叫不起作用:

myListBox.Items.Add("whatever")
myListBox.Items.Add("stuff")
myListBox.Items.Remove("whatever")
我得到一个例外:

{System.InvalidOperationException: Operation not supported on read-only collection.
   at System.Windows.Controls.ItemCollection.RemoveImpl(Object value)
   at System.Windows.Controls.ItemCollection.RemoveInternal(Object value)
   at System.Windows.PresentationFrameworkCollection`1.Remove(T value)
我可以设置ListBox.ItemsSource,但是使用.Items更容易。我创建的列表框如下所示:

let mutable myListBox= new ListBox()
如有任何想法/建议,将不胜感激。谢谢。

我不确定F#语法,但您应该设置ListBox.ItemsSource

如果您创建一个
可观察集合
,然后将其设置为
项目资源
,您可以从集合中添加和删除项目,列表框将自动更新。

我不确定F#语法,但您应该设置ListBox.ItemsSource


如果您创建一个
可观察集合
,然后将其设置为
项目资源
,您可以从集合中添加和删除项目,列表框将自动更新。

以下代码对我来说很好:

open System.Windows.Controls
let l = ListBox()
l.Items.Add("An item")
l.Items.Add("Another item")
l.Items.Remove("An item")

在创建列表框和尝试添加项目之间,您是否正在执行其他操作?

以下代码对我来说很好:

open System.Windows.Controls
let l = ListBox()
l.Items.Add("An item")
l.Items.Add("Another item")
l.Items.Remove("An item")

在创建列表框和尝试添加项目之间,您是否正在执行其他操作?

我将尝试。。。这很奇怪,因为在C#Items.Remove/Items.RemoveAt中似乎起作用:@Mike-同意这很奇怪,但我对F#不够熟悉,无法解释它的不同之处。通过创建一个新的ObservableCollection,将其指定为我的列表框上的ItemsSource,然后处理它而不是Items来实现这一点。仍然不确定为什么项目是只读的,但是,嘿,这是有效的。谢谢。我试试那个。。。这很奇怪,因为在C#Items.Remove/Items.RemoveAt中似乎起作用:@Mike-同意这很奇怪,但我对F#不够熟悉,无法解释它的不同之处。通过创建一个新的ObservableCollection,将其指定为我的列表框上的ItemsSource,然后处理它而不是Items来实现这一点。仍然不确定为什么项目是只读的,但是,嘿,这是有效的。谢谢。除了指定列表框的一些属性(高度等)之外,没有。除了指定列表框的一些属性(高度等),没有。