Replies: 3 comments 13 replies
-
The behavior you see is not normal. You should show a minimal repro project. My pattern for doing viewmodel loading is like this: (Based on new Avalonia project template using CommunityToolkit) and v11.2.3: public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
this.combobox.SelectionChanged += Combobox_SelectionChanged;
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
_ = (DataContext as MainViewModel)!.LoadAsync();
}
private void Combobox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"Selection changed: {e.AddedItems[0]}");
}
} public partial class MainViewModel : ViewModelBase
{
[ObservableProperty]
string[]? _items;
public async Task LoadAsync()
{
var loadedItems = await Task.Run(() =>
{
// Loading large amount of data from http endpoint or file, or doing calculations
// doing it on background thread so UI will not freeze.
return new string[] { "Item 1", "Item 2", "Item 3" };
});
Items = loadedItems;
}
} And in <ComboBox x:Name="combobox" ItemsSource="{Binding Items}" /> The combobox then shows all items, and selecting triggers |
Beta Was this translation helpful? Give feedback.
-
Are you able to share some code? It would make it a lot clearer what is happening. |
Beta Was this translation helpful? Give feedback.
-
Sry, I have to reopen the thread. I had not removed all temporary code and this had "effects" I initially didn't recognize and therefore I was fooled by the code. It was a fake success! I still have the problem that if I am feeding the Moving it into the constructor of the ViewModel, everything is OK. Here my current code (mostly identical, some changes according to the suggestions here, to my first post. But maybe you have some ideas if you see it again. View XAML:
View CS:
ViewModel:
I am not sure if these Nevertheless, I have no idea, why this issue happens. |
Beta Was this translation helpful? Give feedback.
-
Hello,
actually I load data which have to be shown in my
view
/DataGrid
/ComboBox
in the event handler forAttachedToVisualTree
, not in the constructor of the viewmodel.A ComboBox in my view shows via
ItemsSource
a list of entries which where also loaded inAttachedToVisualTree
. But the items cannot be selected. It seems they are not known, even if they are shown by the ItemsPanel. In detail,SelectionChanged
is not triggered, the selected item is not shown in the textpanel, even ifSelectedItem
is set.The datagrid is filled correctly!
When I transfer the loading of the data into the constructor, all items are shown in the
ItemsPanel
and they can be selected, ... means the selected item is then shown in the textpanel,SelectionChanged
is triggered.Is this a bug, that the
ItemsPanel
ofComboBox
is showing items for selection but they cannot be selected effectively? Why isSelectionChanged
not triggered? This seem weird to me.What is better in principle, loading (a big amount of) data in the constructor or in the handler of
AttachedToVisualTree
?-> Avalonia 11.2.3
Beta Was this translation helpful? Give feedback.
All reactions