✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
Hi,
How can I get the value of the selected item in a ComboBox in C# (I'm using AvaloniaUI), please?
10 Replies
Is there a SelectedItem property?
This looks to be it, though it's a generic
object?
type so I'm not exactly sure what you want out of it. Are you looking to get the value of a specific field on the object or do you just need the object? https://reference.avaloniaui.net/api/Avalonia.Controls.Primitives/SelectingItemsControl/F3D6C726There's also the
SelectedIndex
property that could help.
https://reference.avaloniaui.net/api/Avalonia.Controls.Primitives/SelectingItemsControl/3725F15EPrefer using MVVM
And bind the SelectedItem to a property
SelectedItem="{Binding MySelectedItem}"
⏫ Is how you bind it in XAML
MySelectedItem is a property in the Viewmodel
Viewmodel is the object instance in DataContextYes
Thanks, I'll look at it all
Thank You, i succeeded.
if it can help anyone, I did it like this:
Please don't do this.
Avoid code-behind and prefer MVVM.
If you change the name on the control, you must also change name in code-behind. And if you forget to change code-behind it will error and throw an exception.
Ok thank you
and optimization level does it change anything?
I don't know about the performance gain, but generally speaking it is way easier to work on the application and extend it since it follows a structure.
Read more on how to get starged with MVVM here https://intellitect.com/blog/getting-started-model-view-viewmodel-mvvm-pattern-using-windows-presentation-framework-wpf/
AvaloniaUI is made similarly to WPF, so the XAML API generally remains the same except when it comes to styling.
Avalonia also has a guide on MVVM https://docs.avaloniaui.net/docs/concepts/the-mvvm-pattern
which I recommend more than the previous link
thank you very much, I will take a look