S3bt3r
S3bt3r

Reputation: 4169

Why is CommandParameter always null?

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; }
    }
}

Upvotes: 1

Views: 3509

Answers (3)

S3bt3r
S3bt3r

Reputation: 4169

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.

Upvotes: 1

Jehof
Jehof

Reputation: 35544

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}"/>

Upvotes: 0

Sheridan
Sheridan

Reputation: 69959

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?

Upvotes: 0

Related Questions