Reputation: 21186
I have a listbox:
<ListBox Grid.Row="1"
x:Name="TestCasesList"
ItemsSource="{Binding TestCases}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Button Command="{Binding Path=DataContext.ButtonClickCommand_DisplayFailureDetails, ElementName=TestCasesList}"
CommandParameter="{Binding Failures}">
...
I have a view model added to my DataContext:
<Window x:Class="blah.UI.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:design="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
Name="Window">
I have a view model RelayCommand:
ButtonClickCommand_DisplayFailureDetails = new RelayCommand<List<Failure>>( (param) => Execute_ButtonClickCommand_DisplayFailureDetails(param) );
public RelayCommand<List<Failure>> ButtonClickCommand_DisplayFailureDetails
{
get;
private set;
}
private void Execute_ButtonClickCommand_DisplayFailureDetails( List<Failure> failures )
{
Failures = new ObservableCollection<Failure>(failures);
}
The button is not firing my command, any reasons why?
Upvotes: 2
Views: 1305
Reputation: 39386
Try changing the command binding as I show below:
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.ButtonClickCommand_DisplayFailureDetails}" ...>
This way you can get the DataContext
property of your window which was set with an instance of your ViewModel.
Upvotes: 1