C
C#5mo ago
M.

✅ 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
br4kejet
br4kejet5mo ago
Is there a SelectedItem property?
vanille.
vanille.5mo ago
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/F3D6C726
AvaloniaUI
Avalonia - A multi-platform .NET UI framework
vanille.
vanille.5mo ago
AvaloniaUI
Avalonia - A multi-platform .NET UI framework
Buddy
Buddy5mo ago
Prefer 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 DataContext
M.
M.5mo ago
Yes
No description
M.
M.5mo ago
Thanks, I'll look at it all Thank You, i succeeded. if it can help anyone, I did it like this:
c#
private ComboBox ComboBoxOutputFormat;


private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
ComboBoxOutputFormat = this.FindControl<ComboBox>("ComboBoxOutputFormat");
ComboBoxOutputFormat.SelectionChanged += ComboBoxOutputFormat_SelectionChanged;
}

private void ComboBoxOutputFormat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string valeurSelectionnee = (ComboBoxOutputFormat.SelectedItem as ComboBoxItem)?.Content?.ToString();

if (!string.IsNullOrEmpty(valeurSelectionnee))
{
Console.WriteLine($"Option sélectionnée : {valeurSelectionnee}");
}
}
c#
private ComboBox ComboBoxOutputFormat;


private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
ComboBoxOutputFormat = this.FindControl<ComboBox>("ComboBoxOutputFormat");
ComboBoxOutputFormat.SelectionChanged += ComboBoxOutputFormat_SelectionChanged;
}

private void ComboBoxOutputFormat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string valeurSelectionnee = (ComboBoxOutputFormat.SelectedItem as ComboBoxItem)?.Content?.ToString();

if (!string.IsNullOrEmpty(valeurSelectionnee))
{
Console.WriteLine($"Option sélectionnée : {valeurSelectionnee}");
}
}
Buddy
Buddy5mo ago
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.
M.
M.5mo ago
Ok thank you and optimization level does it change anything?
Buddy
Buddy5mo ago
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
M.
M.5mo ago
thank you very much, I will take a look