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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:46:38+00:00 2026-06-04T13:46:38+00:00

I have a DataGrid bound to an ObservableCollection . I add cols dynamically. Cols

  • 0

I have a DataGrid bound to an ObservableCollection. I add cols dynamically. Cols struc is 1st col is TextBlock rest all are Buttons. I have certain issues with the buttons :

I want to set Command for that col, calling a function “OpenTORWindow” with 2 parameters (String, String). I can’t make out how do I set it. Code to add cols is as :

FrameworkElementFactory buttonTemplate = null;
for (int i = 0; i < GlobalUtils.TOR_List.Count; i++)
{                
    buttonTemplate = new FrameworkElementFactory(typeof(Button));                
    switch (i) {
        case 0:
            buttonTemplate.SetBinding(Button.ContentProperty, 
                                      new Binding("CLVButtonText"));
            break;
        case 1:
            buttonTemplate.SetBinding(Button.ContentProperty, 
                                      new Binding("MKBLButtonText"));
            break;
    }
    buttonTemplate.SetBinding(Button.CommandProperty, new Binding("MyCommand"));

    RoutedEventHandler handler = new RoutedEventHandler(OpenNewWindow);
    buttonTemplate.AddHandler(Button.ClickEvent, handler, true);
    this.seivesTorGrid.Columns.Add(new DataGridTemplateColumn()
    {
        Header = GlobalUtils.TOR_List[i].TOR_Id, 
        CellTemplate = new DataTemplate() { VisualTree = buttonTemplate  }
    });                
}

I assign MyCommand with:

MyCommand = new RelayCommand(param => this.OpenWindow(s.SeiveIdSize))

But the MyCommand is never triggered. Then I added AddHandler, that’s working.
Any idea why CommandProperty is not working.

  • 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-06-04T13:46:40+00:00Added an answer on June 4, 2026 at 1:46 pm

    The button you are adding shares the DataContext from the current row in the DataGrid, so when you call ‘MyCommand’, WPF searches the object in TOR_List, and as it probably does not exists, it will not execute.
    You can check the output windows to check for binding errors.

    To achieve what you want, you’ll have to create the command in the object from which TOR_List is a list for, or use RelativeSource.

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

Sidebar

Related Questions

I have a DataGrid which is being bound dynamically to a database query. The
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I have an ObservableCollection bound to a WPFToolkit DataGrid in an MVVM pattern. Every
I have a datagrid bound to an ObservableCollection, and what I'd like to do
I have a WPF DataGrid bound to ObservableCollection . Each item in my collection
I have a DataGrid bound to a ViewModel's property of type ObservableCollection. Inside DataGrid
I have a WPF Toolkit DataGrid bound to an ObservableCollection of Car in my
I have an ObservableCollection that is bound to a DataGrid where I want the
using WPF/C#/Entity Framework. I have a DataGrid.ItemsSource bound to some ObservableCollection . Now, I
I have a datagrid that's bound to an observablecollection object. The collection may be

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.