Search code examples
silverlightdynamicdatagridcolumn

In Silverlight how do you set the HeaderStyle of a dynamic DataGridColumn


I have a data grid and I am dynamically adding columns to this grid from my ViewModel. The user has a settings dialog where they can manage which columns appear in this DataGrid. The problem I'm running into is that I don't know how to set a HeaderStyle on one of these DataGridColumns that are created in my ViewModel.

The most basic version of this would be to be able to add a tooltip to the DataGridColumnHeader for these dynamic columns. Note that these columns are not being defined in XAML because of their dynamic nature. Most of the columns are data-driven and thus aren't known at compile time.

Bonus Points for showing how to add a button to this style and how to setup its click or command property so that I can remove the column from the grid (envision an x image on a button in the column header, when I click this I want to remove the column.)


Solution

  • You can assign a HeaderStyle in code like this:-

            Style headerStyle = new Style(typeof(DataGridColumnHeader));
            headerStyle.Setters.Add(new Setter(ToolTipService.ToolTipProperty, "Hello World"));
    
            yourDynamicColumn.HeaderStyle = headerStyle;