Chrisjan Lodewyks
Chrisjan Lodewyks

Reputation: 1327

Styling from code behind

I would like to style a WPF data-grid from my code behind, as the data-grid is created in code behind.

I have a resource dictionary in my application with all styles.

In XAML I would apply the style to the data-grid as follows:

Style="{StaticResource DataGridStyle}"

How can I achieve this for a data-grid created in code behind as follows:

DataGrid dg = new DataGrid();

Thanks in advance.

Upvotes: 2

Views: 316

Answers (1)

Daniel Sklenitzka
Daniel Sklenitzka

Reputation: 2236

dg.Style = this.Resources["DataGridStyle"] as Style;

Upvotes: 4

Related Questions