C
C#2w ago
voidey

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.
No description
15 Replies
ero
ero2w ago
where do you set the datacontext?
voidey
voideyOP2w ago
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">
ero
ero2w ago
mh, i'm not sure where you're setting datacontext here
MODiX
MODiX2w ago
Klarth
x:DataType is to enforce type safety for compiled bindings.
Quoted by
<@542772576905199626> from #gui (click here)
React with ❌ to remove this embed.
voidey
voideyOP2w ago
Oh, so that means, I need to define it in the code behind, right?
ero
ero2w ago
you don't, luckily :)
<UserControl.DataContext>
<vm:InventoryViewModel />
</UserControl.DataContext>
<UserControl.DataContext>
<vm:InventoryViewModel />
</UserControl.DataContext>
voidey
voideyOP2w ago
thanks, let me try that.
voidey
voideyOP2w ago
still no luck, I am quite puzzled on why it is inferred rightly here but not in the datagrid?
No description
ero
ero2w ago
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)
voidey
voideyOP2w ago
okay, will do thanks @ero
Insire
Insire2w ago
can you post your InventoryViewModel and InventoryItem ? the properties are enough
voidey
voideyOP2w ago
Hey thanks, I slept on the project for a while and fixed the issue using explicit casting to InventoryViewModel, like this:
<Button Content="Edit"
Command="{Binding
((vm:InventoryViewModel)DataContext).EditItemCommand,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}" />
<Button Content="Edit"
Command="{Binding
((vm:InventoryViewModel)DataContext).EditItemCommand,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}" />
voidey
voideyOP2w ago
Here it is
Nasdack
Nasdack2w ago
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/11339
GitHub
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...
voidey
voideyOP2w ago
Oh thanks, that is exactly what I used here.
Want results from more Discord servers?
Add your server