CommandParameter not passing in expected data bound object

Topics: Prism v4 - WPF 4
May 6, 2011 at 9:49 PM
Edited May 6, 2011 at 9:49 PM

I'm using Prism4 and having some difficulty with data binding.

My view has a list box which contains a list of buttons, with the intention of having each button bound to a simple "Slot", here is my xaml:

<ListBox
       ItemsSource="{Binding Slots}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <Button
                            Command= {Binding SlotChangedCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem}"
                            Content="{Binding SlotID}">                    
                        </Button>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

 

And here is my ViewModel code:

private ObservableCollection<Slot> slots;
public ObservableCollection<Slot> Slots
    {
      get
      {
        return slots;
      }
      set
      {
        slots= value;
        FirePropertyChanged("Slots");
      }
    }

private ICommand slotChangedCommand;
 public ICommand SlotChangedCommand
    {
      get
      {
        if (slotChangedCommand == null)
        {
          slotChangedCommand =
            new DelegateCommand<Slot>
              OnSlotChanged );
        }
        return (slotChangedCommand);
      }
    }

public void OnSlotChanged (Slot slot)
    {
      // slot is always null, why?
    }

 

Slot is a simple class with 2 string properties: SlotName and SlotID. The binding works fine and I see my list of buttons with the button text set to their ID's, and when I click on it I can get to the correct Command handling method, however the parameter is always null, can someone point out what I'm doing wrong?

May 9, 2011 at 8:21 PM

Hi,

It could be helpful if you could provide us with a repro sample (for example using SkyDrive) of your solution, so that we can help you find out the cause of the unexpected behavior you're experiencing.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

May 10, 2011 at 3:15 AM

Use this instead of RelativeSource. It doesnt work well in silverlight 3/4.

Your code now look like this

<ListBox
       ItemsSource="{Binding Slots}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <Button Command="{Binding SlotChangedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Tag}"                          				Content="{Binding SlotID}">       
				<BindingHelper.Binding>             
					<BindingProperties TargetProperty="Tag" SourceProperty="SelectedItem" RelativeSourceAncestorType="ListBox"/>
				</BindingHelper.Binding>
                        </Button>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

With Silverlight 5 it would have the FindAncestor to traverse up the visual tree.
Cheers.
May 12, 2011 at 6:59 PM
Edited May 12, 2011 at 7:06 PM

Thanks gan_s for your help, but unfortunately your code didn't work for me.

mbronzovic: thanks for your suggestion, I've uploaded my code sample to SkyDive, this is my first time using it, so please let me know if you have trouble accessing it.

With this code for some reason I'm having trouble even reaching the command handler: 

http://cid-4e8c3454c0c409f3.skydrive.live.com/redir.aspx?resid=4E8C3454C0C409F3!105

Thanks sooo much in advance!!

May 12, 2011 at 8:01 PM

Hi,

In order to trigger the command you define inside the ListBox´s Item Template, you need to access your command that resides in your ViewModel from within the DataTemplate (whose DataContext is overridden), therefore you should implement one of the possibilities described in the following threads:

I uploaded the modified solution here, so you can check the modifications.

Please, let us know if this information helps you.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

May 13, 2011 at 3:24 AM

I'm using it all over my app. You probably have done something wrong. The code definitely works like a charm !

May 13, 2011 at 4:22 PM

Thanks Miguel for looking at the code and modifying it, it works great now!! Thanks so much!!

gan_s, yes I probably was doing something wrong but just wasn't able to figure out how to make it work as your suggested. Thanks nevertheless!!