Solved

How do I create the correct ICommand (or RelayCommand)

Posted on 2010-11-09
4
960 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 1 28
Import Data from Multiple Text Files in Excel 12 58
Iteration Help (Asp.net VB) 5 24
MailAddress in vb 4 29
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 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