This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have recently started using the DataGrid because of it's support for hierarchical data and find that it works very nicely. I have been using the Microsoft DataGrid and have implemented some custom row and cell styling logic there through overriding PrepareContainerForItemOverride in a subclass of DataGridCellsPresenter. I use this to set tooltips on certain cells (with a cell template that puts a red triangle in the upper right corner to notify the user that a tip is there--similar to Excel), to set various text and alignment properties by individual cell or row, set underline for a row, etc. Is the only recommended way to do this sort of stuff with your grid through a DataTemplateSelector that returns data templates with all such properties already set? I would have to make such templates in code, since I can't know in advance what properties might be set. It kind of seems to me that making a new template to set a property to a different value is a bit a pain, and maybe inefficient. Anyway I think I can work that out if it's really the best way. I am using the latest nightly build. Peter |
|
|
Hello Peter Using data templates would be the best way to go about this. Though I am not entirely sure why you'd need to make them in code. Could you use triggers or/and converters to include any runtime logic to change the property values? Also, note that the data template selector properties that the data grid exposes is for providing display-templates and editor-templates for the cell content, such as what editor to use. Providing these such templates would be useful for editor-specific properties such as text alignment which makes sense for text editors, but might not make sense for a ComboBox. I assume you also need a way to modify the actual cell container style to make common changes to all cells, for example displaying the tool tip triangle. In this case, you'll want to copy all the DataGridCellContainerStyle resources from one of our themes, include this in your application resource dictionary and then modify them to include the additional elements. If you need help with doing this, let me know. Jason Fauchelle |
|
|
Hi Jason, I have been able to get the tool tips on cells working, along with the little red triangles, using data templates I built in code. The main reason I think I have to make the data templates in code is that I receive the data to display in the grid in a "data model" that contains "data series" to be displayed as columns in the grid (this is the same data model that we use to supply data to the Chart control). Each series provides a PropertyDescriptor that provides the actual values. From the property descriptor I make a binding for each column (or Chart series) in code. I guess I just don't know how I can make the necessary binding from such a property descriptor in XAML--maybe you can tell me. However, I did notice a new problem: switching to setting Column.DisplayTemplate instead of Column.DisplayMemberBinding seems to have disabled the ability to sort columns. I think I traced this back to a code segment DataGrid.GetColumnsToDisplay that only sets columnInfo.PropertyInfo if there is a DisplayMemberBinding or PropertyName--but does not handle the case of a data template. Should I set the DisplayMemberBinding in addition to the DisplayTemplate? (or I see there is an internal PropertyDescriptorCollection there, and I do have property descriptors for every column that I could provide). Another comment on data templates. The data model I receive can also have background, foreground, various text properties, and row underlining information (including line thickness/color), on a grid, column, row, or cell basis. I can handle the properties specified on a grid or column bases in the data templates as described above. I also see I could add bindings to all the other propeties that could potentially be set. However, this strikes me as very inefficient to add all these extra binding for every cell in the grid for properties that are rarely set. The alternative I guess is to provide a DataTemplateSelector that makes a special data templates for cells as needed. Maybe this is the better choice. Peter |
|
|
Hello Peter Good to hear you have some of this working already. Since you are already building the templates in code, I recommend sticking to this approach since it works. When providing your own DataGridColumn instances, you will in fact need to either set PropertyName or DisplayMemberBinding. The PropertyName will work if your DataGrid ItemsSource is a generic collection, and that generic type provides a list of all the property descriptors. If this is not the case, then you will need to go with setting the DisplayMemberBinding. The DataTemplateSelector idea does sound like a more efficient way in terms of reducing the number of bindings that need to be made. Though I have not tried a complex scenario as this, so you'll need to try it out to see what is best. Jason Fauchelle |
|