Wrong DataContext Inference?
First of all I am still a beginner, so I might not completely know what I am saying. So I am building an Avalonia MVVM app, it is an inventory management system. I have used Postgres for the database. The problem here is, in InventoryView, the commands to edit and delete rows can not be resolved. Rider tells me that the datacontext for the commands is InventoryView, which should be InventoryViewModel, if I am right?
I am very new to Avalonia, and only familiar with Winforms, which a lot different.
15 Replies
where do you set the datacontext?
In InventoryView,
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:IMS.ViewModels"
x:DataType="vm:InventoryViewModel"
x:Class="IMS.Views.InventoryView">
mh, i'm not sure where you're setting datacontext here
Klarth
x:DataType
is to enforce type safety for compiled bindings.Quoted by
<@542772576905199626> from #gui (click here)
React with ❌ to remove this embed.
Oh, so that means, I need to define it in the code behind, right?
you don't, luckily :)
thanks, let me try that.
still no luck, I am quite puzzled on why it is inferred rightly here but not in the datagrid?
at this point i would direct the question to #gui. either close this post or link to here with a short summary of the issue (we don't like crossposting)
okay, will do
thanks @ero
can you post your
InventoryViewModel
and InventoryItem
?
the properties are enoughHey thanks, I slept on the project for a while and fixed the issue using explicit casting to InventoryViewModel, like this:
Here it is
the DataContext inside a DataTemplate isn't inferred from its parent control, which is
InventoryView.axaml
in your case. you can set a name for your view and use that name as ElementName
in your binding parameters, or you can use some special syntax to explicity specify the data binding context, like here:
https://github.com/AvaloniaUI/Avalonia/discussions/11339GitHub
Binding to "parent" data context in ItemsRepeater · AvaloniaUI Aval...
I want to bind some elements of a data item template NOT to items data context but to the "parent" kind of data context. In below sample specifically to a command in the general data cont...
Oh thanks, that is exactly what I used here.