Home Forums WPF controls Xceed DataGrid for WPF How to add a button column

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #27034 |

    Is there a code (not xaml) sample for how to simply add a button with a click handler to a column?

    Imported from legacy forums. Posted by Mike Pateras (had 1354 views)

    User (Old forums)
    Member
    Post count: 23064

    In your Grid Initialization method


     void InitGrid

    {

                FrameworkElementFactory buttonControl = new FrameworkElementFactory(typeof(Button));
                buttonControl.AddHandler(Button.ClickEvent, new RoutedEventHandler(b_Click));
                DataTemplate template = new DataTemplate(typeof(Button));
                template.VisualTree = buttonControl;

                CellEditor cellEdit = new CellEditor();
                cellEdit.EditTemplate = template;

                TestGrid.Columns[0].CellEditor = cellEdit;

    … 

    }

     //Column Button Click event
    private void b_Click(object sender, RoutedEventArgs e)
    {
           MessageBox.Show(“Button Click Event”);
    }

     
    Cheerios,

    Serene
     

    Imported from legacy forums. Posted by Serene (had 1151 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks again, Serene! Where do you learn this stuff?

    Imported from legacy forums. Posted by Mike Pateras (had 655 views)

    User (Old forums)
    Member
    Post count: 23064

    Learnt it via long sessions of hair pulling and banging my head against the computer [:P]

    Nah… I have been using the Xceed DataGrid since quite some time now, with help from the support team and lots of experimenting.. you’ll get a hang of it (somehow) [:)]
     

    Serene
     

    Imported from legacy forums. Posted by Serene (had 890 views)

    User (Old forums)
    Member
    Post count: 23064

    The button isn’t showing up. I noticed you said I should do it in my grid initialization. I setup my combo boxes right after I load my data. Is this not the appropriate place to do the button as well? I tried wrapping the button code with a BeginInit() and EndInit(), which didn’t work, either. This is what I have for the button code:

                FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
                buttonFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));

                DataTemplate buttonTemplate = new DataTemplate(typeof(Button));
                buttonTemplate.VisualTree = buttonFactory;

                CellEditor buttonCellEditor = new CellEditor();
                buttonCellEditor.EditTemplate = buttonTemplate;

                Column column = new Column();
                column.FieldName = “BUTTON”;
                column.Title = “BUTTON”;
                column.CellEditor = buttonCellEditor;

                dataGridControl.Columns.Add(column);

    The column appears, but it has no contents. 

    Imported from legacy forums. Posted by Mike Pateras (had 909 views)

    User (Old forums)
    Member
    Post count: 23064

    For me, the button only appears after the user clicks in the cell.

    Can someone from Xceed shed light as to how one can make it appear immediately?

    Serene 

    Imported from legacy forums. Posted by Serene (had 730 views)

    User (Old forums)
    Member
    Post count: 23064

    You’re right. It does appear after the cell is clicked on, specifically when the cell goes into edit mode.

    Imported from legacy forums. Posted by Mike Pateras (had 721 views)

    Xceed Support
    Member
    Post count: 5658

    You need to change both the CellEditor, which appears when a cell is being edited, and the CellContentTemplate, which is used to display the content of the cell when it is not being edited.

    Edit: Mike beat me by seconds! [:)]

    Imported from legacy forums. Posted by Jenny [Xceed] (had 604 views)

    User (Old forums)
    Member
    Post count: 23064

    I can get it to show up by just setting the CellContentTemplate of the column to the template:

     

                 FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
                buttonFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));

                DataTemplate buttonTemplate = new DataTemplate(typeof(Button));
                buttonTemplate.VisualTree = buttonFactory;

                dataGridControl.Columns[“Name”].CellContentTemplate = buttonTemplate;

     

    Thank you again for your help, Serene! 

    Imported from legacy forums. Posted by Mike Pateras (had 977 views)

    User (Old forums)
    Member
    Post count: 23064

    [quote user=”Jenny”]

    Edit: Mike beat me by seconds! [:)]

    [/quote]

    Thank you, too, Jenny.
     

    Imported from legacy forums. Posted by Mike Pateras (had 1239 views)

    User (Old forums)
    Member
    Post count: 23064

    Is there a way to know in which row (=which business object) was the button clicked?

    Imported from legacy forums. Posted by dasdas (had 1009 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.