Is it possible to bind two different data to the same DataGrid column. Say if I have class A which has the Property p1 and I have another class B with the Property p2. Is it possible to bind p1 and p2 to the same datagrid column?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Something like this:
will work, so long as the classes don’t have any properties with the same name. But that’s a kind of ugly hack, and good luck finding real binding errors among all of the spurious binding errors that this approach will generate.
The mapping of each types’ properties to columns has to live somewhere, but it doesn’t have to live in XAML, and that’s not where I’d put it. I’d do it in my view model. Assuming that I don’t already have view model classes for my ClassA and ClassB objects (and that I don’t want to create them), I’d implement something like this:
…and then bind the
DataGrid‘s columns to a collection ofDataGridHelperobjects.If I did have
ClassAViewModelandClassBViewModelclasses, I’d just implementColumn1,Column2, etc. properties in both. That’d be the way to go if I needed to support two-way binding and validation.