As a proof-of-concept, I created an application that simply displays a
ComboBox that displays the available columns. The user can select a column and press the "Update" button. When the "Update" button is selected, the ComboBox will move to the selected column. The application looks like the following (I know it's ugly, but I've also been playing with some of the LinearGradientBrush stuff). 
The XAML code that is of interest looks like the following (please bear in mind this is just a snippet and not the whole thing):

The Attached Properties in the code snipped used above are
Grid.Row and Grid.Column. These properties define where in the grid the ComboBox is displayed. In order to set the attached property from the code behind, I had to essentially use 1 (yes one) line of code in the Click event of the Button. That line of code looks like the following: Grid.SetColumn(ColumnComboBox, selectedIndex); selectedIndex is just an integer set from the selected value of the combo box.
The "magic" occurs because Attached Properties essentially call static methods of an object. The static method is always named in the same manner. It is defined as
ObjectType.SetNameOfProperty. So from the example, the ObjectType is Grid and the NameOfProperty is Column. Pretty kewl.
No comments:
Post a Comment