A fixed-width column is a column that cannot be resized by mouse dragging or double-clicks. You can find instances in outlook. Currently two methods can be used to achieve the effect. One is to restyle GridViewColumnHeader to remove the gripper inside its Template. The other is to subclass GridViewColumn to restrict columns’ width to a fixed size. Today’s topic is about the second solution. The resulting column is a little bit different from the restyling one because there is still a gripper inside GridViewColumnHeader. When mouse is over the gap between columns, the cursor is still changed to the resizing cursor.
The main WPF elements we are going to cover in this session are GridViewColumn, dependency property definition, dependency property coercion.
Step 1. Subclass GridViewColumn
The key in this step is to override WidthProperty’s metadata to make it call CoerceWidth() to coerce its value when a new value is available.
Step 2. Add a dependency property FixedWidth
The FixedWidth is used to set column’s fixed width in spite of whatever the column’s width is. The keys in this step are:
1. Define a dependency property.
2. When FixedWidth is changed, it calls CoerceValue() to coerce Width into the new value.
Step 3. Rewrite CoerceWidth() to make it always return FixedWidth
Then the Width will always get the same value whatever value is set on Width.
Step 4. Use it in GridView
It is used like normal GridViewColumn.
This sample is based on the February CTP.
Declaimer: This posting is provided “AS IS” with no warranties, and confers no rights.