1

I am creating a list box that holds notes. When a note is selected and double-clicked it opens an editing form. Here there is an option to archive the note. When the note is archived it should not be visible on the original form.

I have tried several things which can be seen below. I cannot seem to find a property that holds the visibility of a single item.

listBox.SelectedItem = Visibility.Collapsed;
listBox.SelectedItem.Visibility.Collapsed;

However they do not work. Any suggestions are appreciated!

Julia
  • 45
  • 1
  • 5
  • How are you opening the form when clicking on the listboxitem? You could pass on the instance of the listboxitem to the form, and from there, tell it to collapse when user archives it. – Jason Roner Aug 01 '19 at 17:01
  • The second form opens after a double click of an item on the listbox. – Julia Aug 01 '19 at 17:25

1 Answers1

1

Try the following:

((ListBoxItem)listBox.SelectedItem).Visibility = Visibility.Collapsed;

listBox.SelectedItem returns the item as an object. You need to type cast this to a ListBoxItem object it allows you to access all of the different properties of a listboxitem.

Hope this helps/works for you :)

* Edit *

The stack-overflow thread, typecasting in C#, should help to explain what I mean by casting. I will also try relate the answer from that thread to this problem.

Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:

// As previously mentioned, SelectedItem returns an object
object x = listBox.SelectedItem;

// We know that x really refers to a ListBoxItem so we can cast it to that.
// Here, the (ListBoxItem) is casting x to a ListBoxItem. 
ListBoxItem y = (ListBoxItem)x;

//This allows us to call the different methods and properties of a listbox item:
y.Visibility = Visibility.Collapsed;

//In my original answer I combined these three lines into one

Hopefully this helps to explain the answer in a bit more detail, there are also plenty of resources out there that can help explain type casting and objects in C# far better than!

Ben
  • 86
  • 2
  • How do I cast it to a ListBoxItem? – Julia Aug 01 '19 at 17:25
  • Sorry, I realise I didn't make that particularly clear in my answer, this was my first time trying to answer a question here! I will edit my answer to make it more clear. The following stack-overflow question should help explain casting in a bit more detail : https://stackoverflow.com/questions/1339482/typecasting-in-c-sharp – Ben Aug 01 '19 at 18:47
  • Thank Ben appreciate the help! :) I ended up modifying my SQL code so that the listbox would not populate the archived notes. – Julia Aug 01 '19 at 19:00
  • Ah ok, that's also a good idea, at least you hopefully know this for future reference in case the issue crops up again :) – Ben Aug 01 '19 at 19:11