question

Christopher-2820 avatar image
0 Votes"
Christopher-2820 asked PeterFleischer-3316 answered

WPF Datagrid: Checkbox and Button stackpanel

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);

                 }
             }
windows-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Christopher-2820 Is there any error message for your question? Or what issue did you meet with the above code? Are dgDaily and dtDailyShift DataGrid? Is there a connection between them? Could you give some details about the design for your xaml?

0 Votes 0 ·

dgDaily is datagrid and dtDailyShift is datatable. dtDailyShift is for saving to Database. There is no error. I just cannot find a right code that would locate any changes I made to dgDaily(datagrid)

0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.