✅ (Avalonia) i dont get data binding
so im following the official tutorial on how to make a simple to-do app with avalonia. everything is fine and dandy but for whatever reason, trying to add a data context doesnt display my hardcoded values i set!
//MainWindow.axaml
<Window //boiler plate stuff
x:DataType="vm:MainWindowViewModel"
x:Class="testAvaloniaApplication1.Views.MainWindow"
Title="Iskander's great and grand TO-DO list">
<Views:ToDoListView DataContext="{Binding ToDoList}"/>
</Window>
//MainWindow.axaml
<Window //boiler plate stuff
x:DataType="vm:MainWindowViewModel"
x:Class="testAvaloniaApplication1.Views.MainWindow"
Title="Iskander's great and grand TO-DO list">
<Views:ToDoListView DataContext="{Binding ToDoList}"/>
</Window>
4 Replies
//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>
//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 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);
}
}
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);
}
}
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};
😁
$close
Use the /close command to mark a forum thread as answered
oh thanks for the heads up!