DataGrid Buttons

Mar 4, 2009 at 3:13 PM
I have a datagrid with a button:

<Custom:DataGrid IsSynchronizedWithCurrentItem="True" Margin="40,120,40,124" Grid.ColumnSpan="3" Name="gvItems">
                <Custom:DataGridTemplateColumn Width="100">
                            <Button Content="Print Label" Click="btnPrint_Click"></Button>

How do I “wire” that button into the data grid so that when that button is clicked row and column information is found and sent to another object?  Kind of like a Windows forms gridview button column.


Mar 4, 2009 at 6:52 PM

Following should work for you....

        T FindVisualParent<T>(UIElement element) where T : UIElement
            UIElement parent = element;
            while (parent != null)
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                    return correctlyTyped;
                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            return null;

        void btnPrint_Click(object sender,...)
            Button button = (Button)sender;
            DataGridCell cell = FindVisualParent<DataGridCell>(button);
            DataGridRow row=FindVisualParent<DataGridRow>(cell);
            DataGridColumn column = cell.Column;
            // use row and column

Mar 5, 2009 at 2:28 PM
What do you have in your using statements??  I have all three parts of the toolkit in my project references (WPFToolKit, WPFToolKit.Design, and WPFToolKit.VisualStudio) but I'm getting an error on DataGridCell, DataGridRow, and DataGridCoulmn. 

The type or namespace name 'DataGridCell' could not be found (are you missing a using directive or an assembly reference?)

I'm not however calling any part of the toolkit through a using statement.  I didn't think I had to.  But I'm pretty new to WPF.



Mar 5, 2009 at 3:17 PM
Edited Mar 5, 2009 at 3:23 PM
Forget it I figured it out.

using Microsoft.Windows.Controls;

For anyone else with the same question.

And thanks!!  That was a big help.  I may get the hang of this WPF stuff after all.


Dec 5, 2009 at 9:12 PM

It took me forever to find this but it was exactly what I needed.