One of the best features of a list box is that you can actually select stuff.
So I can select Mark here, I can select Scott.
But how can we get those values that I've selected?
Right now I can only select one, but and that's the first thing to mention here.
A list box can have three right down for selection mode.
We can set different values, we can set single, multiple and extended.
So multiple, for example, if we just start the application, I just want to show that real quick.
Now select multiple values and that's something that you well could use pretty often depending on the
use case, right?
If I hold down control, I can now select everything and we'll unselect it.
Also use the shift key.
So that's working fine.
Now I have five values selected and it brings us again back to the main to the root question, which
is how can we get those values?
Let's just add a simple button which we can click and then we can show any information to the user regarding
all the elements that he has selected or a single one or whatsoever.
So let's add a button, let's add a click event.
So right now, since this is really just some basic stuff without any kind of real functionality, right?
We're just exploring WPF right now.
I will simply create a new event handler and get the name managed by the system button.
Click is well from my side of you, not the best name, but it's totally fine because it's the only
button that we have.
And for testing purposes, all of that is fine so we can keep it.
Now button click.
We have that here button, click right now.
Let's take a look at our layout because I think that we will not see the button, right?
Let's just create another row where we can add the button.
So let's scroll a little bit up.
Here's our row definition and then we will add two rows, in fact, one with another value of ten go
and another one with a value of 50.
So we will place our button inside the zero one, two, three in the third row.
Let's scroll down.
Let's set it in the grid.
Dot row three grid dot column.
Never forget to set the column one.
Now you can see here is our button.
Let's add a content and we can say Show selected.
For example, any name would be good.
Let's save it here.
Let's switch over to our code behind.
We have the button event, we have our list box of people.
Right?
And then if we want to get all those selected items, we simply write dot and then selected item or
selected items.
Really depending on how you have configured your list box.
Right now we have it right here, selection mode multiple so we can have one or multiple selections.
Let's take a look again.
Selected items is of type list.
So a collection again and selected item is a single one.
It's a single object.
So let's take selected items.
Let's store it inside of a variable.
Var selected items equals to list box.
Right?
So now we have our selected items here.
Var is actually whatever data type selected items is.
Right.
This is a pretty good example of where it makes sense to use the var keyword.
Well, now you can see the autosuggestion here for each var item in selected items, I will simply hit
tab to autocomplete that now we have a for each loop here which is just iterating over the selected
items.
Let's take a look at item here.
You can see it's of type object.
Let's simply take our item here and show it in a message box.
So write down message box dot show.
And what we want to show, first of all, is the item and then the type.
So write down, get type because right now it's of type object, which is not really specific, right?
To make it work, we just write it as a string.
So get type and then dot to string because we want to explore what object type we're getting.
Now pay attention.
If we select multiple items, we get the message box show here inside of a loop.
So we will see if we select more items.
We will see more message boxes, not only one, right, But that's fine for now.
Let's see.
Let's just select two items.
We should see two message boxes.
That's fine.
I select that one.
Hold down control, select another one, click on show selected.
You can see WPF demo data Person.
Awesome.
So whatever we select, we already know all that's a person.
And this is because of the clever well data system or data binding system in WPF.
Now how can we turn that item here which is of type object into a person so that we can work with it
again, right.
Because we cannot access the age property or something like that because it's an item, it's not a person.
So we have to do a cast, right?
So let's cast this into a person.
First of all, I take my var person here to create a person and then I simply write down the parentheses
to cast the item into person object.
Then I can use the message box and write down person dot name, for example, to get the name.
So now we took our object, which could be any type, converted it or casted it into our person class
so that we again have access to the properties, for example, and then we can simply use it because
you can see that it's now of type person.
And if I hit start, we will be able to see the names of those elements that are selected.
Let's take Scott, let's hit show selected.
You can see.
Scott Awesome.
That works fine.
Maria Lucas Without Scott Maria and Lucas show.
Maria Okay.
Lucas There we go.
Very cool.
So yeah, that's basically how you could.
Get access to the selected elements of your list box.