1

In my WPF application I create Datagrid dynamically from code behind. However, I want to have a datagrid with check boxes on row header similar to this.

I know how to do it from XML, but not from cs code. Is there any idea how to handle this situation? ps. my code is very big I cannot put it here but if you need more info, please put comments below. Cheers

1 Answer 1

2

How about something like this:

var dg = new DataGrid();

var dataTemplate = new DataTemplate();

var gridFactory = new FrameworkElementFactory(typeof(Grid));
var checkboxFactory = new FrameworkElementFactory(typeof(CheckBox));
checkboxFactory.SetBinding(CheckBox.IsCheckedProperty, new Binding("IsSelected") { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,typeof(DataGridRow),1)});
gridFactory.AppendChild(checkboxFactory);

dataTemplate.VisualTree = gridFactory;
dg.RowHeaderTemplate = dataTemplate;

Hopefully this should be able to be put into your code without too much effort, probably just have to change the DataGrid name from "dg".

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It works for me. I do this with the XamlReader class because the FrameworkElementFactory class is now deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.