开发者

Why is ListBox.Items.IsReadOnly=true? (F#/Silverlight)

开发者 https://www.devze.com 2023-01-27 21:43 出处:网络
I\'m trying to remove some objects from a ListBox that I have created, and for some reason ListBox.Items.IsReadOnly is true.

I'm trying to remove some objects from a ListBox that I have created, and for some reason ListBox.Items.IsReadOnly is true.

The following calls don't work:

myListBox.Items.Add("whatever")
myListBox.Items.Add("stuff")
myListBox.Items.Remove("whatever")

I get an exception:

{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)

I can set ListBox.ItemsSource, but working with .Items is much easier. I'm creating the ListBox like this:

let mutable myListBox= new ListBox()

Any ideas/suggestions开发者_如何学Python would be greatly appreciated. Thanks.


I'm not sure of the F# syntax, but you should be setting the ListBox.ItemsSource.

If you create an ObservableCollection and then set that to the ItemsSource you can add and remove items from the collection and the list box will update automatically.


The following code works fine for me:

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

Are you doing something else in between the creation of the list box and attempting to add items?

0

精彩评论

暂无评论...
验证码 换一张
取 消