Prev | Current Page 290 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

In fact, the Grid.Row property can be set on an element even if that element
isn??™t in a Grid??”and even if there isn??™t a single Grid object in your element tree.
Instead of using a .NET property wrapper, attached properties require a pair of shared
methods that can be called to set and get the property value. These methods use the familiar
SetValue() and GetValue() methods (inherited from the DependencyObject class). The shared
methods should be named SetPropertyName() and GetPropertyName().
Here are the shared methods that implement the Grid.Row attached property:
Public Shared Function GetRow(ByVal element As UIElement) As Integer
If element Is Nothing Then
Throw New ArgumentNullException(...)
End If
Return CType(element.GetValue(Grid.RowProperty),Integer)
End Function
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 148
Public Shared Sub SetRow(ByVal element As UIElement, ByVal value As Integer)
If element Is Nothing Then
Throw New ArgumentNullException(...)
End If
element.SetValue(Grid.RowProperty, value)
End Sub
Here??™s an example that positions an element in the first row of a Grid using code:
Grid.SetRow(txtElement, 0)
Alternatively, you can call the SetValue() or GetValue() method directly and bypass the
shared methods:
txtElement.SetValue(Grid.RowProperty, 0)
The SetValue() method also provides one brain-twisting oddity.


Pages:
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302