Why is CommandParameter always null?

3.4k views Asked by At

anybody an idea why CommandParameter is always null?

The class TransactionViewModel has the collection property of TransactionCommands to be displayed in the ItemsControl. The items are of type CommandViewModel.

TransactionBrowserViewModel has the command AddJobForSelectedTransactionCommand. The command to be passed as a parameter the CommandViewModel.

View Snipp:

        <ItemsControl Grid.Row="4"
                      ItemsSource="{Binding TransactionCommands}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <telerik:RadButton Content="{Binding DisplayName}"
                                       CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"
                                       Command="{Binding ViewModel.AddJobForSelectedTransactionCommand, ElementName=userControl}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl> 

Codebehind of UserControl:

[Export]
public partial class TransactionBrowserView : UserControl, IView<TransactionBrowserViewModel>
{       
    [ImportingConstructor]
    public TransactionBrowserView()
    {
        InitializeComponent();
    }

    [Import]
    public TransactionBrowserViewModel ViewModel
    {
        get { return (TransactionBrowserViewModel)this.DataContext; }
        set { this.DataContext = value; }
    }
}
3

There are 3 answers

1
Asesjix On

OK, sorry I have found the error. It is located on the RadButton by Telerik. I have tested the scenario with a default button. Here it works without any problems.

4
Sheridan On

You have set the ComandParameter to the path of the DataContext of the RadButton, but I don't see that you have set anything to that DataContext anywhere.

Look into the Output window for information regarding your Binding errors... it should say something like 'There is no DataContext property on object XXX'.

What are you trying to bind to the CommandParameter property?

3
Jehof On

Try this binding

<ItemsControl x:Name="transactionList" Grid.Row="4" ItemsSource="{Binding TransactionCommands}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <telerik:RadButton Content="{Binding DisplayName}"
                         CommandParameter="{Binding SelectedItem, ElementName=transactionList}"
                         Command="{Binding ViewModel.AddJobForSelectedTransactionCommand, ElementName=userControl}"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl> 

Give your ItemsControl (like transactionList) and set the binding of the CommandParameter to the SelectedItem of your transactionList.

or does this not do what you want.

<telerik:RadButton Content="{Binding DisplayName}"
                   CommandParameter="{Binding}"
                   Command="{Binding ViewModel.AddJobForSelectedTransactionCommand, ElementName=userControl}"/>