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 6060087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:47:07+00:00 2026-05-23T08:47:07+00:00

I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind

  • 0

I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind with my View and and EmpCol is set as with ItemSource of custom control. That custom control generates stackpanels with grids , if there are 4 objects in EmpCol then there will be 4 stackpanels with Grids inside them. Now in those grids, I have a column of Buttons.
Now my problem is I can’t bind Command of that Button in datagrid to RelayCommand in Employee class.

I am using MVVM light toolkit, and I found a way around which actually kind of give full path to binding command like this

<Button x:Name="myButton"  Width="20" Height="15" HorizontalAlignment="Center"   VerticalAlignment="Center" Margin="5" >   
<i:Interaction.Triggers>
 <i:EventTrigger EventName="Click"> 
<cmd:EventToCommand Command="{Binding Source={StaticResource VMLocator}, Path=MyVM.Employee.MyButtonCommand}" PassEventArgsToCommand="False"/>  </i:EventTrigger>
</i:Interaction.Triggers>
</Button>

Now this above approach works , but in my case I dont have Employee , but collection of Employee, so when I give binding path like this
Path=MyVM.EmpCol.MyButtonCommand

It does’t work. I have searched a lot , but could’t find any solution.
So any help will be much appreciated to solve my problem.

Thanks,
Maverick

The command binding happens inside a row of a DataGrid.

  • 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-23T08:47:08+00:00Added an answer on May 23, 2026 at 8:47 am

    The tsolution depends on the structure of your view, if you have a master detail view you should create a sub-structure that expects an employee ViewModel as a DataContext and bind that to the currently selected employee. With that you get the correct ViewModel for your view.

    If you navigate to a new page the easiest approach might be to to pass the View the id of the employee you want to show and then in code behind call a method on you view model – which you can get from the DataContext propery – to load the correct employee from the database or service.

    Alternatively, you can hold the current employee in a application wide acessible variable. E.g. a static variable on your application class, a singleton or static class stored in you resources, or on your ViewLocator (in all cases you may have to provide an empty employee ViewModel when no employee is selected or prevent the opening of the View).

    Edit:

    If a collection of ViewModels is bound to a DataGrid and binding of the command is a problem, this might be a possible solution:

    Put the command on a ViewModel that contains the Collection (i.e.the propety that is bound to the ItemsSource of the DataGrid). Then implement the command on this high level ViewModel. You now can use a RelativeSurce binding to access this higher level ViewModel via the DataGrid’s parent DataContext property. In your button you can now bind the command and pass the current row’s DataContext as the command parameter – the command obviously has to expect this parameter.

    Edit 2:

    Forgot Silverlight does not support all RelativeSource modes, however, it supports binding to a named element, so the following should work …

    Assumptions:

    1. The higher level ViewModel is called “EmployeeListViewModel”
    2. It has a property called Employees containing the list of EmployeeViewModels
    3. The DataContext of the element containing the DataGrid is set to EmployeeListViewModel

    Code:

    <DataGrid x:Name="dgEmployees" ItemsSource="{Binding Empoyees}" ...>
        ...
            <Button x:Name="myButton"  Width="20" Height="15" HorizontalAlignment="Center" 
                    VerticalAlignment="Center" Margin="5" >   
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click"> 
                        <cmd:EventToCommand 
                            Command="{Binding ElementName=dgEmployees, Path=DataContext.MyButtonCommand}" 
                            CommandParameter="{Binding}" 
                            PassEventArgsToCommand="False"/>
                </i:Interaction.Triggers>
            </Button>
        ...
    </DataGrid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox which has its ViewModel - let's call it ListBoxViewModel .
I have an App which has a Tasks tab and a Projects tab. I
I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager). each InputviewModel has
I have a TreeView which I have bound to a ViewModel called RootViewModel which
I have an app that has a ListBox of ListBoxes. I would like to
I have a WPF application developed against the MVVM framework in which the ViewModel
I have a view named Work.xaml. This Work.xaml contains multiple WorkSkeleton.xaml. The Work.xaml's ViewModel
I have a listbox in which the item source contains a List(of T) that
I have a ListBox which is populated from a collection of ViewModels, which uses
I have a TextBox.TextProperty bound to a ViewModel property. In the binding I have

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.