Hi Christopher,
instead of "buttonfactoy.AddHandler(Button.ClickEvent, ..." use Command property with ICommand class and CommandParameter bindet to current data item. In Execute method you get as state the item of clicked row.
WPF Datagrid: Checkbox and Button stackpanel
Christopher
1
Reputation point
Hello,
I coded in xaml.cs my datagrid with a single column of checkbox and button using factory and stackpanel. Right now I want to detect what column and row they are and if the checkbox ischecked = true. My column headers on datagrid are dynamic based on the dates range they chose
This is my sample code:
int extra = 1;
while (startDate < endDate.AddDays(1))
{
string combo1 = "combo" + extra;
var col2 = new DataGridTemplateColumn();
string colname = startDate.ToShortDateString() + " [" + Utils.ToDateTime(startDate).ToString("ddd") + "]";
col2.Header = colname;
var template2 = new DataTemplate();
var sp = new FrameworkElementFactory(typeof(StackPanel));
sp.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
var buttonfactoy = new FrameworkElementFactory(typeof(Button));
col2.CellTemplate = template2;
dgDaily.Columns.Add(col2);
dtDailyShift.Columns.Add(startDate.ToShortDateString(), typeof(string));
buttonfactoy.SetValue(Button.HeightProperty, 35.0);
buttonfactoy.SetValue(Button.ContentProperty, "CLICK FOR SHIFT");
buttonfactoy.SetValue(Button.MarginProperty, new Thickness(5));
buttonfactoy.SetResourceReference(Button.StyleProperty, "ButtonPrimary");
buttonfactoy.SetResourceReference(Button.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
// A column named 'System.Windows.Controls.DataGridTemplateColumn' already belongs to this DataTable.'
buttonfactoy.AddHandler(Button.ClickEvent, new RoutedEventHandler((o, f) => HandleColumnButtonClick(o, f)));
sp.AppendChild(buttonfactoy);
template2.VisualTree = sp;
extra++;
startDate = startDate.AddDays(1);
}
}
1 answer
Sort by: Most helpful
-
Peter Fleischer (former MVP) 19,326 Reputation points
2020-11-25T12:28:04.87+00:00