I am using the Model View ViewModel pattern in my WPF application. The DataContext of my view is set to the ViewModel.
I have a ListView in my view, which has a ContextMenu and one of the MenuItems there needs to bind to a Command and the CommandParameter is the ListView itself.
Now my problem is, that I don't know how to reference the ListView. Maybe a code-snippet makes it easier to understand:
<ListView
Name="lvTestList"
ItemsSource="{Binding Path=TestList.Items}">
<!-- Context Menu of the selected test -->
<ListView.ContextMenu>
<ContextMenu>
<MenuItem
Header="Remove from List"
IsEnabled="{Binding IsATestSelected}"
Command="{Binding RemoveTestFromTestListCommand}"
CommandParameter="{Binding ElementName=lvTestList}"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding TestName}"/>
<GridViewColumn Header="Package" DisplayMemberBinding="{Binding PackageName}"/>
<GridViewColumn Header="Expected Duration" DisplayMemberBinding="{Binding ExpectedDuration}"/>
</GridView>
</ListView.View>
</ListView>
The problematic line is the one saying:
CommandParameter="{Binding ElementName=lvTestList}"/>
Normally, this would be working. However, if the DataContext of the whole class changes, it just passes null as parameter.
Does anyone know how to keep a reference to the current xaml document? or how to "talk" to the ListView "lvTestList" directly?
Best wishes and Thanks for help, Christian