WPF - Pass Command Parameter to RelayCommand  - Need a simple example

Posted on 2012-08-23
Last Modified: 2012-08-23
I need a working example of how to pass a parameter to a Relay Command.

I have simple hyperlink button. When the button is clicked I need to pass a string parameter to the command - the hyperlink will then open a document using the parameter passed

  <Hyperlink Command="{Binding Open_Doc_Command}" CommandParameter="{Binding Path=somefilename}" >
                                            Open Document
Question by:JElster
    1 Comment
    LVL 14

    Accepted Solution

    Something like the following?

    private RelayCommand m_Open_Doc_Command;
    public System.Windows.Input.ICommand Open_Doc_Command
    		if (m_Open_Doc_Command == null)
    			m_Open_Doc_Command = new RelayCommand(param => Open_Doc(param));
    		return m_Open_Doc_Command;
    private void Open_Doc(object obj)
    	string filename = obj as string;
    	// do something

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now