Hello!
We need to show a button within a single cell. This depends on the bound field. If the bound object is of a certain type, a button needs to be shown. But this cannot be set for the field (column) because this may happen only once in a column and once in a row.
I was playing around with CellValuePresenter, but I was not able to change the control within the cell.
I did not find a solution? Does anyone has an idea?
Thanks in advance!
Florian
Hello Florian,
Retemplating the CellValuePresenter is the right approach to take if you want to change the content of a cell and embed a control. You can attach a sample project or provide us with the code that you are trying and we can look into it.
Hey Alex!
Please find the sample project attached.
I create a mixed list of DataObjectInt and DataObjectString. The list is attached to the DataSource property of the XamDataGrid.
The grid should display the DataObjectInt as a label with a blue background. Is not working!
The DataObjectString should displayed as a button. Is also not working. For the sake of simplicity I added a property "ShowButton" to the data objects. But in my real world scenario, the decision whether the cell is displayed as button or not needs to be made depending on the type of the data object.
Thanks for further assistance,
I am attaching your project back to you modified.
In your sample, you have two instances of different classes, which means that the XDG will create two field layouts. You are using AutoGenerateFields-false so you need to create two field layouts. In your sample you have created only one, and this is why the style is not applied. The other seems to be the DynamicResource markup ext.
Hi Alex,
thanks, that's the solution. But let me ask two more questions on this:
1. How can I assign which field layout is used for which class?
2. Is there a way to hide the addtional column headers that are shown in the middle of the grid? We only want to have one header at the top of the grid.
Thanks, Florian
Florian,
Yes, you can achieve 1 and 2 in one event. You have to use the AssignFieldLayoutToItem event. There are couple of properties in the event args that can help you determine which field layout you want to assign to that item. Define all of the fieldlayouts in the xaml (as you have done) and then assign them accordingly in this event (e.FieldLayout = ... ).
Thanks for your quick reply and thanks for the good answer!