Silverlight/XAML – Learning by Coding
1: <?xml version="1.0" encoding="UTF-8"?>
2: <!-- coded by Thomas Meinike 08/08 -->
3: <UserControl x:Class="sl2_datagrid.Page"
4: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6: xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
7: xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
8: Width="800" Height="600">
9:
10: <UserControl.Resources>
11: <Style TargetType="dataprimitives:DataGridColumnHeader" x:Key="DataGridHeader">
12: <Setter Property="HorizontalContentAlignment" Value="Center"/>
13: <Setter Property="VerticalContentAlignment" Value="Center"/>
14: <Setter Property="FontSize" Value="16"/>
15: </Style>
16:
17: <Style TargetType="TextBlock" x:Key="DataGridCell">
18: <Setter Property="VerticalAlignment" Value="Center"/>
19: <Setter Property="TextAlignment" Value="Center"/>
20: <Setter Property="FontSize" Value="14"/>
21: </Style>
22: </UserControl.Resources>
23:
24: <Canvas>
25:
26: <data:DataGrid Canvas.Left="30" Canvas.Top="30" x:Name="dgrid"
27: AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeColumns="False"
28: HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
29: ColumnWidth="110" ColumnHeaderHeight="25" RowHeight="20"
30: RowBackground="#6CF" AlternatingRowBackground="#F9C"
31: GridLinesVisibility="All" HeadersVisibility="All"
32: ColumnHeaderStyle="{StaticResource DataGridHeader}">
33:
34: <data:DataGrid.Columns>
35:
36: <data:DataGridTemplateColumn Header="x">
37: <data:DataGridTemplateColumn.CellTemplate>
38: <DataTemplate>
39: <TextBlock Text="{Binding x}" Style="{StaticResource DataGridCell}"/>
40: </DataTemplate>
41: </data:DataGridTemplateColumn.CellTemplate>
42: </data:DataGridTemplateColumn>
43:
44: <data:DataGridTemplateColumn Header="y = x^2">
45: <data:DataGridTemplateColumn.CellTemplate>
46: <DataTemplate>
47: <TextBlock Text="{Binding y}" Style="{StaticResource DataGridCell}"/>
48: </DataTemplate>
49: </data:DataGridTemplateColumn.CellTemplate>
50: </data:DataGridTemplateColumn>
51:
52: <data:DataGridTemplateColumn Header="y = x^3">
53: <data:DataGridTemplateColumn.CellTemplate>
54: <DataTemplate>
55: <TextBlock Text="{Binding z}" Style="{StaticResource DataGridCell}"/>
56: </DataTemplate>
57: </data:DataGridTemplateColumn.CellTemplate>
58: </data:DataGridTemplateColumn>
59:
60: </data:DataGrid.Columns>
61:
62: </data:DataGrid>
63:
64: </Canvas>
65:
66:
67: <!-- // zusätzlich verwendeter VB.NET-Code in Page.xaml.vb:
68:
69: Partial Public Class Page
70: Inherits UserControl
71:
72: Public Class Data
73: Private _xwert As Integer
74: Private _ywert As Integer
75: Private _zwert As Integer
76:
77: Property x() As Integer
78: Get
79: Return _xwert
80: End Get
81: Set(ByVal value As Integer)
82: _xwert = value
83: End Set
84: End Property
85:
86: Property y() As Integer
87: Get
88: Return _ywert
89: End Get
90: Set(ByVal value As Integer)
91: _ywert = value
92: End Set
93: End Property
94:
95: Property z() As Integer
96: Get
97: Return _zwert
98: End Get
99: Set(ByVal value As Integer)
100: _zwert = value
101: End Set
102: End Property
103: End Class
104:
105: Public Sub New()
106: InitializeComponent()
107:
108: Dim Source As List(Of Data) = New List(Of Data)
109: Dim Anzahl As Integer = 10
110:
111: For i As Integer = 1 To Anzahl
112: Source.Add(New Data() With {.x = i, .y = i * i, .z = i * i * i})
113: Next
114:
115: dgrid.ItemsSource = Source
116: End Sub
117:
118: End Class
119:
120: -->
121:
122: </UserControl>
[zum Anfang]