Solved

How do I create the correct ICommand (or RelayCommand)

Posted on 2010-11-09
4
955 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now