AIBMass
asked on
How do I create the correct ICommand (or RelayCommand)
I want to create:
AllCustomers as ObservableCollection(Of Customer)
where Customer is:
Public Class Customer
Public Property ID as integer
Public Property Name as String
Public Property Cmd as ICommand (or RelayCommand)
End Class
So that when AllCustomers is bound to the ItemSource of a ListBox
and the Name and ID are shown via a data template, I can click the Name or ID and go to a routine where I can know what the ID of the customer is.
I know this is doable and I probably need another class that will inherit from ICommand or RelayCommand, but I don't understand WPF commanding well enough to do it.
Can someone help?
AllCustomers as ObservableCollection(Of Customer)
where Customer is:
Public Class Customer
Public Property ID as integer
Public Property Name as String
Public Property Cmd as ICommand (or RelayCommand)
End Class
So that when AllCustomers is bound to the ItemSource of a ListBox
and the Name and ID are shown via a data template, I can click the Name or ID and go to a routine where I can know what the ID of the customer is.
I know this is doable and I probably need another class that will inherit from ICommand or RelayCommand, but I don't understand WPF commanding well enough to do it.
Can someone help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had already found this article, but it was not as easy to follow as I hoped.
Nonetheless, it contained the correct info and I was able to figure it out.
Nonetheless, it contained the correct info and I was able to figure it out.
>> ... I can click the Name or ID and go to a routine where I can know what the ID of the customer is.
Is the requirement to call the routine every time a selection is made in the list?
If so you can bind the Listbox's SelectedItem property to a Customer-type property in your class - SelectedCustomer in the example below.
The setter is then called whenever the selection is changed , passing the Customer instance as the new value, and there's no need for a command.
SelectedItem="{Binding SelectedCustomer}">
Is the requirement to call the routine every time a selection is made in the list?
If so you can bind the Listbox's SelectedItem property to a Customer-type property in your class - SelectedCustomer in the example below.
The setter is then called whenever the selection is changed , passing the Customer instance as the new value, and there's no need for a command.
SelectedItem="{Binding SelectedCustomer}">
ASKER
Thx