Solved

How do I create the correct ICommand (or RelayCommand)

Posted on 2010-11-09
4
957 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:AIBMass
  • 2
4 Comments
 

Author Comment

by:AIBMass
ID: 34098668
I guess I should have added that I'm starting with a dataset of customer info....looping through the rows building my collection....and wondering how to build the command.

Thx
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 34101383
you need to inherit from ICommand and create a command of your own, take a look at this tutorial for more info

http://www.codeproject.com/KB/WPF/wpfcommands.aspx


once you have your command, you can bind these to the button's command property ,(you may need to modify the style of the button so that it would look less like a button and more like a label in ur case)
0
 

Author Closing Comment

by:AIBMass
ID: 34101919
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.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 34103482
>> ... 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}">
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now