jeudi 21 juillet 2016

WPF datagrid - how to set checkbox in template column without binding?

I have a datagrid, which is populated programatically by data retrieved from a webservice. The last column of the datagrid contains checkboxes, which allow to show each rows value on a map. The column is created with a template column like this:

                DataGridTemplateColumn column = new DataGridTemplateColumn();
                column.Header = "Show on map";
                DataTemplate dt = new DataTemplate();
                FrameworkElementFactory checkBoxFactory = new FrameworkElementFactory(typeof(CheckBox));
                checkBoxFactory.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckBoxClicked), true);
                dt.VisualTree = checkBoxFactory;
                column.CellTemplate = dt;
                DataGrid.Columns.Add(column);

If the users clicks on one of the checkboxes, the CheckBoxClicked method is invoked and displays the corresponding row values as an icon on the map (as already mentioned). This works very well. However, the user may close the window containing the datagrid, whereas the map stays on the screen, together with the displayed icons. Now, when the user reopens the window with the datagrid, I'd like to preselect the checkbox, according to the already showing icons. Since the checkboxes do not have a binding, I'm unable to match the icons to them. How can this be done? Is there a way to programatically preselect them? Or is there still a way to create a binding?

Any help is appreciated!

Harry




Aucun commentaire:

Enregistrer un commentaire