-
Hi, I have a sample project at https://github.com/just-ero/DataGridTest. In this project, I have this public class Person(string name, int age)
{
public string Name { get; set; } = name;
public int Age { get; set; } = age;
} And I have this viewmodel: public class PersonGridViewModel
{
public ObservableCollection<Person> People { get; } = [
new("John Doe", 25),
new("Jane Doe", 30),
];
} As you can see, there is data persent. Here are the relevant <UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:DataGridTest.ViewModels"
x:Class="DataGridTest.Views.PersonGrid"
x:DataType="vm:PersonGridViewModel">
<Design.DataContext>
<vm:PersonGridViewModel />
</Design.DataContext>
<DataGrid ItemsSource="{Binding People}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
</UserControl> <Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="using:DataGridTest.Views"
xmlns:vm="using:DataGridTest.ViewModels"
x:Class="DataGridTest.Views.MainWindow"
x:DataType="vm:MainWindowViewModel">
<v:PersonGrid />
</Window> As far as I can tell, everything should be set up correctly. However, there's simply no data in the grid at runtime. The grid shows up, but it's just the header. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
use DevTools F12 to inspect DataContext is correctly set |
Beta Was this translation helpful? Give feedback.
-
Design.DataContext is only for design time (previewer), You need to actually assign your DataContext (PersonGridViewModel) to your view (PersonGrid). Either by assigning in xaml
or in code behind
|
Beta Was this translation helpful? Give feedback.
Design.DataContext is only for design time (previewer), You need to actually assign your DataContext (PersonGridViewModel) to your view (PersonGrid). Either by assigning in xaml