Wednesday, December 4, 2013

Modifying styles in code

I have a requirement to dynamically set the maxlength property of an editable column of a datagrid based on a property based deep inside an object. I decided to do this in code so I found the column object and tried to add a setter to the EditingElementStyle. I got an error message that indicated that the style was already sealed and I could not modify it. What to do?

The answer is to create a new style based on the old one, modify the new style, and then assign it to the EditingElementStyle. A bit of a headache but not the hardest piece of WPF coding I've done today. This is what it looks like.

Dim Col As DataGrid.Columns(2) 
Dim Style As New System.Windows.Style(GetType(TextBox), Col.EditingElementStyle)
Style.Setters.Add(New Setter(TextBox.MaxLengthProperty, AccountStructure.Section(2).MaxLen))
Col.EditingElementStyle = Style

No comments:

Post a Comment