Replies: 4 comments 3 replies
-
You need to use some form of binding indirection through a control that contains the VM as its <ItemsRepeater x:Name="items" ItemsSource={Binding ACollectionOnTheViewModel }>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<Button Command="{Binding #items.DataContext.SomeCommand}" /> <!-- With ReflectionBinding -->
<Button Command="{Binding #items.((vm:SomeViewModel)DataContext).SomeCommand}" /> <!-- With CompiledBinding -->
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater> |
Beta Was this translation helpful? Give feedback.
-
fyi: TemplateParent is only for ControlTemplates, where you replace the entire look of a control in your own Style. Using named element would be what I suggest also, or use the |
Beta Was this translation helpful? Give feedback.
-
In the past in situations like this, I've broken layer barriers slightly, and developed a ViewDataAdapter that turned a Data Model into a View Data Model, that had all the properties, commands, timers, subscriptions...whatever, on it the view needed. Has anyone done something like this in Avalonia? |
Beta Was this translation helpful? Give feedback.
-
Thanks chaps, I had two nested ItemsControl and the trick to solve the problem is to define an x:Name on the outer one. |
Beta Was this translation helpful? Give feedback.
-
So, I think this should be easy, but I just can't work out the syntax. Here's the setup
I've been round and round on using the TemplatedParent which throws an error about being in a data template, and trying to use the RelativeSource and ancestors to back up to the ViewModel which is on the UserControl DataContext.
Any help would be appreciated, even if you tell me I'm architecting the solution wrong and I need to rework it to make it work without code stinking' hacks.
Beta Was this translation helpful? Give feedback.
All reactions