✅ Working on a WPF Project, how can I get what StackPanel is selected?
Working on a WPF Project, how can I get what StackPanel is selected by the user and save it to use it in my class?
79 Replies
I want to implement it here:
Please let me know, if you need more information.
Selected? Panels don't have a "selected" mode.
Did you mean focused?
StackPanels in Xaml
I put the wrong code in the first one, my bad.
there is no
IsSelected
property on a stack panel
I don't know how else I can put this. They can't be selected. They are not selectable.
Did you mean IsFocused
?Uhm, maybe I'm calling it the wrong thing 😅
When I click on it, it says selected
With this code
Ah, I see
you have a ListView with each row being populated with a stack
its the ListViewItem that has the
IsSelected
property
not the panel itselfAh, okay.
Can I somehow track which Item is selected and use it in my function?
absolutely
Awesome!
Mind helping me a little? 😅
you're using MVVM, so just bind the
SelectedItem
property on the list itselfI'm kinda lost when it comes to C#... 😅
Mostly followed a tutorial to come to where I'm at on this project and I roughly understand what I'm doing.
I know how to bind it, so I can change the value of the element but not the otherway around. 😅
Do you maybe mind helping? :)
Well, you know how to bind it, so do that first
The value, right?
I obviously don't mind helping, as I already am helping? 😛
what value?
SelectedItem
on your listviewTotally lost, I would not be binding the value of IsSelected?
wh..
what are you doing?
Those are not MVVM bindings
Good question 😂
Where am I doing that? 😭
well, first you make a property in your viewmodel
that will hold the bound value
SelectedItem
on ListView is of type object
, so you might need the binding to also be object
?in ProtectionViewModel.cs
Correct?
string
is not correctbool?
object
Pobiega
SelectedItem
on ListView is of type object
, so you might need the binding to also be object
Quoted by
<@105026391237480448> from #Working on a WPF Project, how can I get what StackPanel is selected? (click here)
React with ❌ to remove this embed.
Never worked with an object?
Is it for true or false?
or?
... what the fuck
is this your first day of C#?
what are you doing making WPF apps
Good question.
WPF with MVVM isn't really beginner level topics lol
Yeah I kinda figured out 😂
Well.
I have the object now.
I've used it for WinForms a lot before, but never made any console apps etc.
okay, now you need to set up a binding
<ListView SelectedItem="{Binding SelectedItem}">
Am I stupid or is it now how my code is formatted?
Also this would be needed, right?
not really
thats only if you need a two-way binding
Oh well 😅
I assume you dont actually want to force set items in the view
Yeah, correct.
so if you just want to know whats selected, a one way is fine
Alright.
you keep showing your style
I dont care about your style
where is your ACTUAL xaml?
Ahh
here is mine
and here is my viewmodel
There we go
note that I'm using a helper package to not have to write the annoying parts 😄
that what the attributes do for me
Alright
oh okay, you're actually using a proper ItemSource
nice
Yeah 👍
I know what that means. ;)
so the
SelectedItem
property is now actually populated by your ServerModel
instance that is the source for your row
from here
So I would need a variable in here?
for what?
Nvm 😅
you have access to all the data in there already
Okay
for the selected row
Ah okay
So I can use the Binding from inside the Server Model now?
yes, its the
SelectedItem
property
I'd set it to object?
btw
since technically nothing might be selected
You could even add a helper property now
public ServerModel? SelectedServerModel => (ServerModel?)SelectedItem;
that way you don't need to cast SelectedItemUhm I'm completely lost again 😂
How do I implement it now?
I want it to check which country is selected and then change the dependent on the Item selected.
This right?
I'm so confused
Me included. 😅
you are sharing only snippets and tiny screenshots of parts of your code
its a nightmare to work like that
Sure
I can share the whole thing.
or just, y'know, do what I say
public string? SelectedServerModel => (string?)SelectedItem;
add that to your viewmodel
err
not with string, but ServerModel
then when you want to check what config is selected, you try to access SelectedServerModel
and check that its not null, and then access the .Config
property on the servermodelEasier said than done... 😅
Can I share the whole project in an easy way?
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
but honestly, all the hard parts are done
you just need to add a property and access it
thats literally it
BlazeBin - mpuspbgpqkrn
A tool for sharing your source code with the world!
Where is this supposed to go?
in
your
viewmodel
But it still doesn't show the selected item... 🤔
It's still this.
I have maybe 5 minutes available. Can you screenshare and we just fix this super fast, and then you promise me to stop doing WPF and learn C# before resuming?
Sorry, Still available?
Promise you this is toture for me 😂
#dev-vc-1