0

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

Community
  • 1
  • 1
  • 2
    Is your code correct? You bind ItemsSource on your ListBox with explicit items declared inside it? Second, does your DataContextSpy class has "DataContent" property? If not, your CommandParameter does not work as expected. – Bill Zhang Jul 02 '13 at 20:24

2 Answers2

0

A good tool for investigating xaml and databinding is Snoop. http://snoopwpf.codeplex.com/

Looking at your code you should define a DataTemplate for your ListBox that contains your TextBlock and CheckBox. You can then use the ItemTemplate property on the ListBox and set it to your data template. This way each ListBoxItem will have a DataContext of type Project.

<DataTemplate x:Key="ProjectTemplate">
<TextBlock...../>
<CheckBox....../>
</DataTemplate>

<ListBox ItemTemplate="{StaticResource ProjectTemplate}" ...... />

Therefore, you want to bind your CommandParameter to a property of your Project (like ProjectID) (when you have a collections of Porjects in your ListBox) I think this is the xaml you will need.

CommandParameter="{Binding Path=DataContext.ProjectID, 
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}"
Darlene
  • 808
  • 5
  • 16
0

I recommend you the MVVM Light Toolkit which is the best framework for MVVM & WPF.

http://mvvmlight.codeplex.com/

More information on http://www.galasoft.ch/mvvm/.

Mateusz Dembski
  • 427
  • 1
  • 5
  • 15