0

It's the code from YouIandReactiveUI

Is there a more elegant way to achieve this? Or is there any way to bind in code with IViewFor?

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <local:TweetMediaAttachmentView ViewModel="{Binding .}"/>
                            <Button
                                x:Name="removeButton"                                                                
                                Command="{Binding DataContext.RemoveAttachmentCommand, ElementName=attachmentsItemsControl}" />                                                            
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
TripleAccretion
  • 312
  • 3
  • 10
ye4241
  • 11
  • 1

2 Answers2

0

This is a known problem. One solution is to use RelativeSource, as proposed here: Access parent DataContext from DataTemplate

TripleAccretion
  • 312
  • 3
  • 10
0

Couple different options, all of which would use the IViewFor functionality.

First is when using the ReactiveUI's OneWayBind functionality to bind to the ItemsControl.ItemsSource, if you don't set a ItemTemplate it will search for the associated IViewfor<View model>.

Another you can use the ItemTemplate and use ViewModelViewHost control and set the view model. This is almost identical to the first approach.

For the command binding I would pass the command into the view model so you don't have to worry about the data context binding.

Glenn Watson
  • 2,758
  • 1
  • 20
  • 30