C
C#2d ago
Dyda

✅ 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.
No description
No description
14 Replies
Buddy
Buddy2d ago
Do not do DataContext.Foo But just directly type Foo
FusedQyou
FusedQyou2d ago
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:
<ItemsControl x:Name="Items" ...
<ItemsControl x:Name="Items" ...
Then, at the binding, refer to that name when defining the binding, so you can then reference the view model using a cast:
<Button Command="{Binding #Items.((TasksPageViewModel)DataContext).EditTaskCommand}"/>
<Button Command="{Binding #Items.((TasksPageViewModel)DataContext).EditTaskCommand}"/>
It's a bit messy. I read a while ago they aim to improve this syntax (if they haven't already)
Dyda
DydaOP2d ago
i tried it datacontext.foo but it doesn't work too
FusedQyou
FusedQyou2d ago
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}"
Dyda
DydaOP2d ago
So you are thinking about: Command="{ Binding DataContext.ChangeStatusCommand, ElementName="Grid" }". If I'm understanding good it doesn't work too, I tried it.
FusedQyou
FusedQyou2d ago
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
Dyda
DydaOP2d ago
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
FusedQyou
FusedQyou2d ago
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 it
Dyda
DydaOP2d ago
Yeah, you're right. I changed command to {Binding #Grid.((vm:OrderListViewModel)DataContext).ChangeStatusCommand} and now its working. Thank you
Keswiik
Keswiik2d ago
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
<controls:EditableListView Grid.Row="0" Grid.Column="0" EditableItemSource="{Binding Datasets}">
<controls:EditableListView.EditableItemTemplate>
<DataTemplate DataType="items:DatasetItemViewModel">
<datasetControls:DatasetSummaryTile DataContext="{Binding }"/>
</DataTemplate>
</controls:EditableListView.EditableItemTemplate>
</controls:EditableListView>
<controls:EditableListView Grid.Row="0" Grid.Column="0" EditableItemSource="{Binding Datasets}">
<controls:EditableListView.EditableItemTemplate>
<DataTemplate DataType="items:DatasetItemViewModel">
<datasetControls:DatasetSummaryTile DataContext="{Binding }"/>
</DataTemplate>
</controls:EditableListView.EditableItemTemplate>
</controls:EditableListView>
slightly better example since it directly makes use of properties on the inherited type:
<ItemsRepeater Width="450" ItemsSource="{Binding ThumbnailButtons}">
<ItemsRepeater.Layout>
<StackLayout Spacing="5" Orientation="Vertical" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate DataType="search:SearchViewThumbnailButtonViewModel">
<Button Command="{Binding OnClickCommand}">
<Image RenderOptions.BitmapInterpolationMode="HighQuality" Source="{Binding Thumbnail}"/>
</Button>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
<ItemsRepeater Width="450" ItemsSource="{Binding ThumbnailButtons}">
<ItemsRepeater.Layout>
<StackLayout Spacing="5" Orientation="Vertical" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate DataType="search:SearchViewThumbnailButtonViewModel">
<Button Command="{Binding OnClickCommand}">
<Image RenderOptions.BitmapInterpolationMode="HighQuality" Source="{Binding Thumbnail}"/>
</Button>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
FusedQyou
FusedQyou2d ago
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
Keswiik
Keswiik2d ago
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 directly
FusedQyou
FusedQyou2d ago
Neat, definitely something I'll use myself from now on @Dyda $close
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?