1

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; }
    }
}
Asesjix
  • 3,891
  • 3
  • 16
  • 19

3 Answers3

1

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.

Asesjix
  • 3,891
  • 3
  • 16
  • 19
  • I had a similar issue where a button style was consuming the event that passed a command parameter. Once I changed up the button, I noticed the parameter came through as the right object instead of null. – h4le5torm Jan 16 '20 at 14:36
0

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?

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • In the output window are no bindingerror to find, therefore also no binding errors should be present. The command should be run directly, but first to be added to a CompositeCommand. – Asesjix Sep 02 '13 at 08:46
  • I've tried following binding CommandParameter = "{Binding}". It doesn't work either. – Asesjix Sep 02 '13 at 08:50
  • Are you sure that you have your WPF Trace Settings on even? Go to Tools > Options > Debugging > Output Window > WPF Trace Settings and at least set Data Binding to Warning or Error. Guessing binding paths also doesn't usually help... think... what object do you want to set as the `CommandParameter`? Does the binding have access to that object? Is it in the same scope as the `Command`? – Sheridan Sep 02 '13 at 08:56
  • I have checked the settings once again. It is set to warning. As all other bindings in the ItemsControl will work, I'm assuming that there should be no access issues. – Asesjix Sep 02 '13 at 09:07
0

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}"/>
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Thanks for your post. That won't work for my scenario, because the button commands has a CanExecute method and bind the individual elements are not selected. – Asesjix Sep 02 '13 at 09:02
  • I tried CommandParameter = "{Binding}". It does not work, is also null. – Asesjix Sep 02 '13 at 09:03
  • 1
    I have the exact same code on two different views (same viewmodel same XAML). It works on one and gives me null on the other. Something is broken. – Jordan Sep 15 '15 at 14:26