-
I tried to do it with Command="{Binding NavigateCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" but it didn't help. Shows Error Code: AVLN2000 (unable to resolve property or method of name NavigateCommand) I also tried Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" but same Error Code: AVLN2000 (unable to resolve property or method of name NavigateCommand) What I am doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To access a different The easiest is to go for the <Button Command="{Binding $parent[ItemsControl].((vm:DashboardViewModel)DataContext).NavigateCommand}">
</Button> You can also look for the <UserControl
xmlns:views="using:Avalonia.UI.Views"
x:Class="Avalonia.UI.Views.DashboardView">
<ScrollViewer>
<ItemsControl>
<ItemsControl.ItemTemplate>
<Button Command="{Binding $parent[view:DashboardView].((VM:TestViewModel)DataContext).TestCommand}">
</Button>
</ItemsControl.ItemTemplace>
</ItemsControl>
</ScrollViewer>
</UserControl> The Avalonia documentation covers this case in a bit more detail here with some additional examples of how to access parent controls in a binding. |
Beta Was this translation helpful? Give feedback.
To access a different
DataContext
from the one you are in, you need to walk up the logical tree until you get to a control which has theDataContext
that you want. You can use the $parent[] syntax to search up the logical tree for a control of a specific type.The easiest is to go for the
DataContext
of the control that your template is part of, like this:You can also look for the
UserControl
. However theUserControl
actually has the type of the class that you named it as seen inx:Class
, not the typeUserControl
so you need to make sure you include the namespace that the