使用WPF创建Office 2007功能区风格的自定义面板
WPF,Windows Presentation Foundation,是Microsoft在.NET 3.0中引入的新组件,提供了表现层技术基础,用于统一处理用户界面和可视化元素。WPF的出现,使得用户开发具有震撼力的用户界面体验更加轻松和可能。同时,WPF是基于XAML(可扩展应用程序标记语言)来定义界面元素的。
下面是《Windows Presentation Foundation Unleashed》一书中使用WPF技术创建仿Office 2007 功能区的自定义面板的示例,所实现的结果如下图所示:
同时,记录下部分代码:
1: Imports System
2: Imports System.Windows
3: Imports System.Windows.Controls
4:
5: Namespace CustomPanel
6: Public Class RibbonPanel
7: Inherits Panel
8: Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size
9: If Children.Count < 1 Then
10: Return New Size(0, 0)
11: End If
12:
13: ‘ Ask the first child for its desired size, given unlimited space
14: Dim firstChild As UIElement = Children(0)
15: firstChild.Measure(New Size(Double.PositiveInfinity,Double.PositiveInfinity))
16:
17: ‘ If there’s only one child, this panel would like to be the exact same size
18: If Children.Count < 2 Then
19: Return firstChild.DesiredSize
20: End If
21:
22: ‘ If not, calculate the desired width based on all children
23: Dim numRows As Double = Math.Ceiling((Children.Count - 1) / 3d)
24: Dim maxWidthForEachRemainingChild As Double = 0
25:
26: Dim i As Integer
27: For i = 1 To Children.Count- 1 Step i + 1
28: ‘ Ask each child for its desired size, given unlimited space
29: Dim child As UIElement = Children(i)
30: child.Measure(New Size(Double.PositiveInfinity,Double.PositiveInfinity))
31:
32: ‘ Keep track of the maximum width
33: maxWidthForEachRemainingChild = Math.Max(child.DesiredSize.Width, maxWidthForEachRemainingChild)
34: Next
35:
36: Return New Size(
37: firstChild.DesiredSize.Width + maxWidthForEachRemainingChild * numRows, ‘ total width
38: Dim ‘ height = desired height of the first child As firstChild.DesiredSize.Height)
39: End Function
40:
41: Protected Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size
42: If Children.Count < 1 Then
43: Return finalSize
44: End If
45:
46: ‘ Give the first child its desired width but the height of the panel
47: Dim firstChild As UIElement = Children(0)
48: Dim childOrigin As Point = New Point(0,0)
49: Dim firstChildSize As Size = New Size(firstChild.DesiredSize.Width,finalSize.Height)
50: firstChild.Arrange(New Rect(childOrigin,firstChildSize))
51:
52: If Children.Count < 2 Then
53: Return finalSize
54: End If
55:
56: ‘ Determine the size for all the remaining children
57: Dim numRows As Double = Math.Ceiling((Children.Count - 1) / 3d)
58: Dim childSize As Size = New Size((finalSize.Width - firstChildSize.Width) / numRows,finalSize.Height / 3)
59: childOrigin.X += firstChildSize.Width
60:
61: Dim i As Integer
62: For i = 1 To Children.Count- 1 Step i + 1
63: Dim child As UIElement = Children(i)
64: child.Arrange(New Rect(childOrigin,childSize))
65:
66: If i % 3 = 0 Then
67: ‘ Start a new column
68: childOrigin.X += childSize.Width
69: childOrigin.Y = 0
70: Else
71: childOrigin.Y += childSize.Height
72: End If
73: Next
74:
75: ‘ Fill all the space given
76: Return finalSize
77: End Function
78: End Class
79: End Namespace












