desktop.MainWindow = new MainWindow{ DataContext = new MainWindowViewModel()};
//ToDoListView.axaml<UserControl //boiler plate stff x:Class="testAvaloniaApplication1.Views.ToDoListView" x:DataType="vm:ToDoListViewModel"> <DockPanel> <Button DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Content="Add an item"/> <ItemsControl ItemsSource="{Binding ListItems}"> <ItemsControl.ItemTemplate> <DataTemplate> <CheckBox Margin="4" IsChecked="{Binding IsChecked}" Content="{Binding Description}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DockPanel></UserControl>
using testAvaloniaApplication1.DataModels;namespace testAvaloniaApplication1.ViewModels{ public class MainWindowViewModel : ViewModelBase { //Depends on ToDoListServices public ToDoListViewModel ToDoList { get; } public MainWindowViewModel() { var service = new ToDoListService(); ToDoList = new ToDoListViewModel(service.GetItems()); } }}
using System.Collections.Generic;using System.Collections.ObjectModel;using testAvaloniaApplication1.DataModels;namespace testAvaloniaApplication1.ViewModels;public class ToDoListViewModel : ViewModelBase{ public ObservableCollection<ToDoItem> ListItems { get; set; } public ToDoListViewModel(IEnumerable<ToDoItem> items) { ListItems = new ObservableCollection<ToDoItem>(items); }}