✅ Problem with binding (#AvaloniaUI)
Hello,
I'm encountering an issue and could use some help. I'm getting the following error message:
"Unable to resolve property or method of name 'ChangeStatusCommand' on type 'System.Object'. Line 30, position 12."
It seems to indicate that the ChangeStatusCommand doesn't exist, but I can't figure out why. Other bindings work fine; the issue only occurs within the context menu of the data grid. Any guidance on what might be causing this would be greatly appreciated.


14 Replies
Do not do DataContext.Foo
But just directly type Foo
I don't think that works either since they are in a data template
The binding is on the Status at that point and not the view model
What I usually do is define a name higher up, for example on the Window or user control or just somewhere outside of the template:
Then, at the binding, refer to that name when defining the binding, so you can then reference the view model using a cast:
It's a bit messy. I read a while ago they aim to improve this syntax (if they haven't already)
i tried it datacontext.foo but it doesn't work too
See what I wrote, you have to get a proper reference outside the data template
So in your case give DataGrid a name like
Grid
, and then change your command to Command="{Binding #Grid.((OrderListViewModel)DataContext).ChangeStatusCommand}"
So you are thinking about: Command="{ Binding DataContext.ChangeStatusCommand, ElementName="Grid" }". If I'm understanding good it doesn't work too, I tried it.
No, I'm not. You're still trying to DataContext directly which doesn't have typing for your command as it's an object type
It's also entirely different from what I suggested
"it doesn't work" is also not helpful. What is the error? You should try what I suggested
I changed dataGrid name to "Grid" and changed command to: Command="{Binding #Grid.((OrderListViewModel)DataContext).ChangeStatusCommand}". Now error look like this: Unable to resolve type OrderListViewModel from namespace https://github.com/avaloniaui Line 31, position 51.
GitHub
Avalonia UI
A cross-platform UI framework for .NET providing a flexible styling system and supporting Windows, Linux, macOS, iOS, Android and Browser. - Avalonia UI
Then you have to make sure you pass the correct namespace to the type, or you have to define the namespace globally
You already did the former by defining
vm
for view models so I believe you can do #Grid.((vm:OrderListViewModel)DataContext)
I generally advice you just define it globally, though
In your case it doesn't work because it now expect it to be defined in Avalonia in general since you don't have that and prefixing with vm:
fixes itYeah, you're right. I changed command to {Binding #Grid.((vm:OrderListViewModel)DataContext).ChangeStatusCommand} and now its working. Thank you
Just want to add that you can specify a
DataType
argument for your DataTemplate
which should let you avoid casting
quick example from one of my projects
slightly better example since it directly makes use of properties on the inherited type:
Nice, definitely a lot cleaner if possible to be used
Though this appears to replace the bindings in general? So any item specific bindings are lost
This is to access item-specific bindings without having to do ugly casting
in the second example,
ThumbnailButtons
and OnClickCommand
come from two different VMs
SearchViewModel.ThumbnailButtons
is an ObservableCollection<SearchViewThumbnailButtonViewModel>
collection, and the DataType
argument for my template lets me access SearchViewThumbnailButtonViewModel.OnClickCommand
and SearchViewThumbnailButtonViewModel.Thumbnail
directlyNeat, definitely something I'll use myself from now on
@Dyda $close
If you have no further questions, please use /close to mark the forum thread as answered