views:

210

answers:

1

Assuming the following view model definition:

public class MyObject {
public string Name { get; set; }

}

public interface IMyViewModel { 
ICommand MyCommand { get; }
IList<MyObject> MyList { get; }

}

And a UserControl with the following code behind:

public class MyView : UserControl {
public IMyViewModel Model { get; }

}

If my Xaml looked like this:

<UserControl>
<ListBox ItemsSource="{Binding MyList}">
 <ListBox.ItemTemplate>
  <TextBlock Text="{Binding Name}" />
  <Button Content="Execute My Command" cmd:Click.Command="{Binding Path=MyCommand, ?????????}" cmd:Click.CommandParameter="{Binding}" />
 </ListBox.ItemTemplate>
</ListBox>

How can I bind my Button to the ICommand property of my code-behind class?

I'm using Prism and SL 3.0 and I need to bind each button in my list box to the same command on my view model. Before my UserControl had a specific name and I was able to use the ElementName binding, but now my UserControl is used multiple times in the same parent view so I can't use that technique anymore and I can't figure out how to do this in xaml. If it is my only option I can do it manually in the code-behind, but I'd rather do it declaratively in the xaml, if possible.

Thanks in advance.

+1  A: 

You need a DataContextProxy for this to work because you're no longer in the context of the UserControl. You've moved out of that and there is no good way to reach back into that context without something like the DataContextProxy. I've used it for my projects and it works great.

Bryant
And this is why I love stackoverflow...
TravisWhidden