?
Solved

How do I create the correct ICommand (or RelayCommand)

Posted on 2010-11-09
4
Medium Priority
?
970 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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