I have a WPF application using MVVM. I have a view with a View Model as its DataContext. Inside the View, I have a ListView binding to an observable collection. On each row of the ListView, I have a button that I want to bind to an ICommand in the DataContext. How do I set the RelativeSource to get to the DataContext ICommand?
xaml
<ListView ItemsSource="{Binding Services}"> <ListView.View> <GridView> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Frequency" DisplayMemberBinding="{Binding Frequency}" /> <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" /> <GridViewColumn Header="Enable State" DisplayMemberBinding="{Binding EnableState}" /> <GridViewColumn Header="" > <GridViewColumn.CellTemplate> <DataTemplate> <Button Background="Transparent" BorderThickness="0" Command="{Binding RemoveTimerClick}" CommandParameter="{Binding}"> <Image Source="pack://application:,,,/Resources/Images/Delete.png" Width="20"/> </Button> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> This Command binding is looking for a the ICommand in the Services. I tried
Command="{Binding RemoveTimerClick, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" But this didn't work because it is binding to the UserControl (which is the View), not the DataContext (which is the View model). How do I bind to the DataContext that also contains the Services collection?