1

I have a button on my usercontrol

<Button Grid.Row="13" Grid.Column="7" Content="Save" Name="btnSave" 
            CommandParameter="{Binding CompanyDetails}"
            Command="{Binding SaveCommand}" />

SaveCommand is a RelayCommand declared in the ViewModel. CompanyDetails is a property in the same ViewModel. The mentioned above ViewModel is the datacontext of my UserControl.

    Public Class CompanyViewModel
    Inherits ViewModelBase
    Implements IDataErrorInfo

    Private _myData As DataFromDatabase      
    Private _companyDetails As New CompanyDetailsBase
    Private _saveCommand As RelayCommandWithParameter

    Public Property CompanyDetails() As CompanyDetailsBase
        Get
            Return _companyDetails
        End Get
        Set(ByVal value As CompanyDetailsBase)
            _companyDetails = value
            OnPropertyChanged("CompanyDetails")
        End Set
    End Property

    Public Property SaveCommand() As RelayCommandWithParameter
        Get
            If _saveCommand Is Nothing Then
                Dim saveAction As New Action(Of Object)(AddressOf Me.SaveIt)
                _saveCommand = New RelayCommandWithParameter(saveAction) ', Me.CanSave)
            End If
            Return _saveCommand
        End Get

        Set(ByVal value As RelayCommandWithParameter)
            _saveCommand = value
        End Set
    End Property

    Public Sub New()
        _myData = New DataFromDatabase(My.Settings.ConnectionString)
        _saveCommand = New RelayCommandWithParameter(New Action(Of Object)(AddressOf SaveIt))
    End Sub

    Public Function GetCompanyDetails(ByVal sap As String) As CompanyDetailsBase
        CompanyDetails = _myData.GetCompanyDetails(sap)
        Return CompanyDetails
    End Function

    Public Sub SaveIt(ByVal c As CompanyDetailsBase)
        MessageBox.Show("Save customer " & c.Sap)
    End Sub

    End Class

The other fields on the usercontrol are bound to an ObjectDataProvider, which calls the GetCompanyDetails function. The CompanyDetails is set right each time this function is called, until I call SaveIt subroutine. When SaveIt is called its parameter is always Nothing. Where do I make the mistake? Thank you a lot for answers.

kzub
  • 232
  • 3
  • 13

2 Answers2

0

In the constructer of CompanyViewModel populate the CompanyDetails with your GetCompanyDetails function. Change the constructor as bellow,

Public Sub New()
    _myData = New DataFromDatabase(My.Settings.ConnectionString)
    _saveCommand = New RelayCommandWithParameter(New Action(Of Object)(AddressOf SaveIt))
    _companyDetails = GetCompanyDetails()  
End Sub

This might help. Good luck.

Rizvi Hasan
  • 703
  • 1
  • 5
  • 19
0

It's been a while since I did VB, and I never did it with WPF, but I think I know what your problem is.

The way you are initializing the Command, you aren't actually passing the parameter in.

When I do it in c# it looks like this =>

_saveCommand = new RelayCommandWithParameter(
            (param) =>
            {
                SaveIt((int)param);
            },
               // this line is the same as your commented out Me.CanSave
            (param) => { return this.CanDisplaySelectedPolicy; }); 

So you can see that in the lamda expression I pass a parameter called param, that is then put into the SaveIt method.

When you create your new action, try putting a lamda expression in it like the one above, so you can pass a parameter into the SaveIt method.

Jason Ridge
  • 1,868
  • 15
  • 27