Skip to content

Plz Help! How to bind Button Command to UserControl DataContext from ItemsControl in Avalonia? #18422

Answered by tobyfirth
IftikharK asked this question in Q&A
Discussion options

You must be logged in to vote

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 the DataContext 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:

<Button Command="{Binding $parent[ItemsControl].((vm:DashboardViewModel)DataContext).NavigateCommand}">
					
</Button>

You can also look for the UserControl. However the UserControl actually has the type of the class that you named it as seen in x:Class, not the type UserControl so you need to make sure you include the namespace that the

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@IftikharK
Comment options

Answer selected by IftikharK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants