1

Currently I have in xaml:

<ItemsControl ItemsSource="{Binding Facilities, Mode=OneWay}">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <Border Style="{StaticResource BorderStyleHeader}">
    <Grid>
     <Grid.RowDefinitions>
      <RowDefinition Height="33" />
      <RowDefinition Height="33" />
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
      <ColumnDefinition Width="150" />
      <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="1" FontWeight="Bold" />
     <TextBlock Text="{Binding Description}" Grid.Row="1" Grid.Column="1" />
     <Button Content="Reserveer Nu" Style="{StaticResource ButtonStyle}" 
             Margin="5" Grid.Row="1" Grid.Column="0" 
             Command="{Binding Reservation.ItemClicked}" 
             CommandParameter="{Binding FacilityId}"/>
    </Grid>
   </Border>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

Now the first thing is that I wish for the button to raise the event in my viewmodel

public RelayCommand ItemClicked
{
    get
    {
        return new RelayCommand(() =>
        {
            MessageBox.Show("Something is clicked");
        });
    }
}

but it refuses...
secondly I want to be able to raise the event with a parameter (notice the commandparameter) but I have never used it and thus I dont understand how to use it.

So my questions:

  1. Why isn't my relaycommand being executed?

  2. How do I make use of the command parameter?

Indy9000
  • 8,651
  • 2
  • 32
  • 37
Theun Arbeider
  • 5,259
  • 11
  • 45
  • 68
  • 1
    "but it refuses": what does that mean? Are you getting a compile error? An exception? Do you see binding errors in the output window? There is no way to answer you question with the information you provide – Thomas Levesque May 03 '11 at 09:20
  • 1
    And which RelayCommand implementation are you using? It's not a standard class, there are multiple implementations – Thomas Levesque May 03 '11 at 09:22
  • Using RelayCommand from MVVM Light (GalaSoft) – Theun Arbeider May 03 '11 at 10:11
  • @Levisaxos, I updated my answer. But you didn't answer my first question... – Thomas Levesque May 03 '11 at 10:18
  • Ah sorry, it refuses to ever reach the relaycommand... – Theun Arbeider May 04 '11 at 06:17
  • @Levisaxos, did you check the binding errors in the output window? – Thomas Levesque May 04 '11 at 08:01
  • Thomas, thanks for pointing that out, I never knew that was there. It says there's no binding found in Facility. So it's looking for the relaycommand in the Facility Entitie instead of the ViewModel. Thou i'm still unsure how to fix this. – Theun Arbeider May 04 '11 at 11:07
  • Where is the Reservation property defined? In the main ViewModel of the view? Just give a name to the root of your view (e.g. "root") and change your binding to "{Binding DataContext.Reservation.ItemClicked, ElementName=root}" – Thomas Levesque May 04 '11 at 14:01

1 Answers1

4

Why isn't my relaycommand being executed?
How do I make use of the command parameter?

If you're using the RelayCommand class from Josh Smith's article, both questions have the same answer... The constructor takes an Action<object>, not an Action. So your code should be:

    return new RelayCommand((param) =>
    {
        MessageBox.Show("Something is clicked - Parameter value = " + param);
    });

EDIT: OK, so you're using the RelayCommand from MVVM Light... There are two versions of this class, one is generic and one is not. The non-generic version doesn't accept a parameter, so you need the generic version. Your command should look like this:

public RelayCommand<int> ItemClicked
{
    get
    {
        return new RelayCommand<int>((i) =>
        {
            MessageBox.Show("Something is clicked - Parameter value is " + i);
        });
    }
}

(assuming the parameter is of type int)

As for why your current code doesn't work, I can't answer without additional information...

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758