Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:56:38+00:00 2026-05-27T20:56:38+00:00

FINAL NOTE Final solution found in another post Although I appreciated the clarification that

  • 0

FINAL NOTE
Final solution found in another post

Although I appreciated the clarification that was provided, the ultimate solution was in-fact provided by another solution as linked above. No matter WHAT I tried, the binding via the “Element Name” component was not working. I had to go based on the “Relative” hierarchy up the datagrid…

<Button Name="btnPrintReport" 
   Command="{Binding DataContext.MyPrintCommand, 
             RelativeSource={RelativeSource FindAncestor, 
                             AncestorType={x:Type DataGrid}}}"
   CommandParameter="{Binding}"
   Height="16" Width="16" HorizontalAlignment="Center" >
   <Image Source="MyButtonImage.png" IsHitTestVisible="True"/>
</Button>

Hope something not too complicated in WPF / MVVM environment. Here’s the scenario.

I have a Window (.xaml) and a corresponding View Model (.cs). The form displays fine with all the data bindings no problem. (note: this is NOT done via any commercial “framework” )

One of the controls that is in the view window is a custom user control of a datagrid with all pre-defined columns, headings and content to be displayed when the view is shown. This works all no problem even though the control is not directly “defined” in the main window .xaml file, but just dropped on the form as the user control itself (which has its own obvious .cs code-behind).

With the main window’s “DataContext” pointing to the View Model, and the user control that has a datagrid

<DataGrid AutoGenerateColumns="False" 
    Name="dataMyStuff"
    ItemsSource="{Binding Path=MyTablePropertyOnViewModel, 
        NotifyOnSourceUpdated=True, 
        NotifyOnTargetUpdated=True}" ... />

Now, what I’m looking for. On this data grid, I have a column that has an image in the first column. When I click on this image, I want to print a report specific to the record as represented by this row (it has a PK value I use). So, how do I tell the image “KeyUp” event to go to the View Model event handler since that is where the data is, and some other methods I’ll need for preparing the call to the report. The view portion of the grid is for cosmetic display to the user, and thus no “functionality” directly in this control.

— EDIT — per progress from answers

I’ve adjusted my datagrid per comments from Josh and Rachel, however, something still does not appear to be quite right… Seeing the button was using a “Command” instance, I interpreted this as it needed to attach to an instance of an “ICommand” interface object on my view model. So, I created an instance. I know the command handler works as it is also used for common things like Add, Edit, Save, Cancel, Exit, etc… So I have a new one for this printing purpose. For simplicity, I have it created as an Always Execute, so there is no method to handle the “CanExecute” portion of the control. I’ve set the button’s “Command” to almost all iterations I could think of an still nothing, but here’s an update of what I’m seeing going on.

<UserControl>
   <Data grid columns / template, etc to the button>
      <DataTemplate> 
         <Button Name="btnPrintReport" 
            Command="{Binding DataContext.MyPrintCommand}" >
            <Image Source="myPrintImage.png"/>
         </Button>
      </DataTemplate>
   </Data grid columns, etc>
</UserControl>

In my ViewModel class (myICommandButtonHandler inherits from ICommand)

private myICommandButtonHandler myPrintCommand;
public myICommandButtonHandler MyPrintCommand
{
   get { if (myPrintCommand == null)
            myPrintCommand = new myICommandButtonHandler(myPrint);
         return myPrintCommand;
       }
}

private void myPrint()
{
   MessageBox.Show( "Doing the print job..." );
}

Now, what I’m seeing. During step through initialization of all the controls and such. I click menu item to call my Window to be displayed. FIRST, it creates an instance of the View Model controller. THEN, it calls the Window and passes in the View Model controller as parameter so it is immediately set at the Window level as the “DataContext” of the window. The main window then goes into it’s “InitializeComponents” call and starts to build out all the other controls embedded, including this separate class that contains the data grid in question. At the constructor of this usercontrol (that has the datagrid), there is no “data context” set yet as the rest of the controls have not been initialized yet, and I don’t know why / when the “bindings” apparently get “hooked” in. So, it appears that trying to do the binding to the data grid’s command button are failing. HOWEVER, at run-time, the actual data IS updated in the grid, so I know that much is working.

So, the data grid has its “ItemsSource” set to a property of a “DataView” property on the view model, but the binding of the “button” doesn’t appear to see the “MyPrintCommand” handler that I thought would get the hit.. and its action is to just display a message box (for now).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:56:39+00:00Added an answer on May 27, 2026 at 8:56 pm

    Usually I use an AttachedCommand Behavior which allows me to bind Events to ViewModel Commands. For example, you could use

    <Image ...
           local:CommandBehavior.Event="KeyUp"  
           local:CommandBehavior.Command="{Binding DataContext.PrintCommand, ElementName=dataMyStuff}"
           local:CommandBehavior.CommandParameter="{Binding }"/>
    

    I’d would recommend using a different event than KeyUp, since I don’t think Images can have Keyboard focus so the KeyUp event will never get fired.

    A better alternative is to use a Button and overwrite it’s Template to be your Image. This will maintain the Click functionality, and give you access to Command and CommandParameter properties

    <Button Command="{Binding DataContext.PrintCommand, ElementName=dataMyStuff}"
            CommandParameter="{Binding }">
        <Button.Template>
            <Image ... />
        </Button.Template>
    </Button>
    

    Also, the CommandParameter="{Binding }" will simply pass the current DataRow’s DataContext (your data object) to the command

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit : Note that my final purpose here is not having the class working,
While making some final tests of a class-library that I'm writing for Windows Mobile
Please note that I have already been through similar questions and their answers here
Update (21st Sept 2016) - Thanks to Digbyswift for commenting that this solution still
It often happens that a single C# solution contains some projects that are x86-specific
Final question for the night. And apologies for the complete noobness of this. I
Can final keyword be used for a method?
The final images produced by compliers contain both bin file and extended loader format
My final goal is to write the program which can run on the Unix
While in the final throws of upgrading MS-SQL Server 2005 Express Edition to MS-SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.