I have a button on a listview control. I have bound this control to one of the command on the base class of the ViewModel class. If I place a button outside of the listview it works fine with the same command. But the command does not get fired when I place it on the listview.
can you think of a reason????
Below is the snippet:
<ListView Grid.Row="2" AlternationCount="2" ItemsSource="{Binding Path=AObject}" Margin="20" MaxHeight="200">
<ListView.DataContext>
<local:MyViewModel/>
</ListView.DataContext>
<ListView.View>
<GridView>
<GridViewColumn Header="Run ID" DisplayMemberBinding="{Binding Path=RID}" />
<GridViewColumn Header="Job ID" DisplayMemberBinding="{Binding Path=JID}" />
<GridViewColumn Header="Run Description">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Path=OpenScCommand}" HorizontalAlignment="Right"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Edit">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding ShowItemCommand}" CommandParameter="{Binding Path=RID}" Content="_Edit email run" IsDefault="False"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
That’s because the button is in the
ListViewItem, so it inherits the DataContext of the item that contains it. Here’s how you can bind to the DataContext of theListViewitself:As a side note: depending on what the command does, it might be better to put it in the ViewModel of the items