I am new to WPf, MVVM and this is my first question in stackoverflow. I will get on to my problem and then I will go on to tell what I have found so far about it.
In the view I have a listbox which is bound to a list say ProjectList which has ProjectId, Name, Description and IsFavourite(bool). Inside the items template I have given a checkbox.
Once the checkbox is checked I am supposed to execute a command which is working fine till here. But the problem is I have to send the ProjectId as the commandparameter. I am failing here.
I have tried using datacontextspy from this link. Different Datacontext for Command and CommandParameter
here I am able to get the datacontext of the Viewmodel and not the ProjectId of the item whose checkbox was checked.
Here is my .xaml code.
<ListBox Name="lstProjects" ItemsSource="{Binding ProjectList}" HorizontalAlignment="Stretch" SelectedItem="{Binding SelectedProject}">
<ListBox.Resources>
<local:DataContextSpy x:Key="spy"/>
</ListBox.Resources>
<TextBlock Grid.Column="1" Grid.Row="1" Grid.RowSpan="3"
Text="{Binding ProjectDescription}" FontSize="10" TextWrapping="Wrap" />
<CheckBox Name="chkFavourites" Grid.Column="1" Grid.Row="1" Content="Favoutires"
CommandParameter="{Binding Path= DataContext,Source={StaticResource spy}}"
Command="{Binding Path=IsSelected}" DataContext="{Binding DataContext,
RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}">
</CheckBox>
</ListBox>
can some on help me on this. Let me know if I am missing any details here.
Thanks in advance, Prashanth