How to: Create or Modify a Style
- 3 minutes to read
Create Your Own Custom Style
Create a New Style
- Add a new style to the Workbook.Styles collection by calling the StyleCollection.Add method with the style name passed as a parameter. This method returns the Style object corresponding to the created style. This object’s Style.Name property is set to the specified name. Other settings are identical to the Normal built-in style.
- Use properties of the newly created Style object to set the required format characteristics.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/wpf-spreadsheetcontrol-api-part-1-e4938.
// Add a new style under the "My Style" name to the Styles collection of the workbook. Style myStyle = workbook.Styles.Add("My Style"); // Specify formatting characteristics for the style. myStyle.BeginUpdate(); try { // Set the font color to Blue. myStyle.Font.Color = Color.Blue; // Set the font size to 12. myStyle.Font.Size = 12; // Set the horizontal alignment to Center. myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center; // Set the background. myStyle.Fill.BackgroundColor = Color.LightBlue; } finally { myStyle.EndUpdate(); }
End-users can select an existing custom style from the collection of available styles, and use it to format cells.
Create a New Style Based on a Built-In Style
- Add a new style to the Workbook.Styles collection by calling the StyleCollection.Add method with the style name passed as a parameter. This method returns the Style object corresponding to the created style. This object’s Style.Name property is set to the specified name. Other settings are identical to the Normal built-in style.
- To copy all format settings from a Microsoft® Excel® built-in style to the newly created style, use the Style.CopyFrom method with the built-in style id (a member of the BuiltInStyleId enumeration) passed as a parameter.
- Use properties of the newly created Style object to change the required format settings of the style.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/wpf-spreadsheetcontrol-api-part-1-e4938.
// Add a new style under the "My Good Style" name to the Styles collection. Style myGoodStyle = workbook.Styles.Add("My Good Style"); // Copy all format settings from the built-in Good style. myGoodStyle.CopyFrom(BuiltInStyleId.Good);
Modify an Existing Style
- Access a style to be modified. To do this, get the corresponding Style object from the Workbook.Styles collection by the style name or index.
- Use the style’s Formatting.BeginUpdate and Formatting.EndUpdate paired methods to modify the required format attributes of the style.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/wpf-spreadsheetcontrol-api-part-1-e4938.
// Change the required formatting characteristics of the style.
myGoodStyle.BeginUpdate();
try {
myGoodStyle.Fill.BackgroundColor = Color.LightYellow;
myGoodStyle.Fill.FillType = FillType.Gradient;
myGoodStyle.Fill.Gradient.Stops.Add(0, Color.AliceBlue);
myGoodStyle.Fill.Gradient.Stops.Add(1, Color.Yellow);
// ...
}
finally {
myGoodStyle.EndUpdate();
}