XAMLの階層構造
(1)以外は標準生成コードです。x:Name="grid1"はClass MainWindowのコード記
載上必要となります。
Class MainWindowクラスの全コード
Class MainWindow
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
grid1.ColumnDefinitions.Add(New ColumnDefinition() With {.Width = New GridLength(100)})
grid1.ColumnDefinitions.Add(New ColumnDefinition() With {.Width = New GridLength(1, GridUnitType.Star)})
grid1.RowDefinitions.Add(New RowDefinition() With {.Height = New GridLength(1, GridUnitType.Auto)})
grid1.RowDefinitions.Add(New RowDefinition() With {.Height = New GridLength(1, GridUnitType.Auto)})
grid1.RowDefinitions.Add(New RowDefinition() With {.Height = New GridLength(1, GridUnitType.Auto)})
Dim label1 As New Label() With {.Content = "Slider:"}
label1.SetValue(Grid.RowProperty, 0)
label1.SetValue(Grid.ColumnProperty, 0)
grid1.Children.Add(label1)
Dim slider1 As New Slider()
slider1.SetValue(Grid.RowProperty, 0)
slider1.SetValue(Grid.ColumnProperty, 1)
grid1.Children.Add(slider1)
Dim label2 As New Label() With {.Content = "Input:"}
label2.SetValue(Grid.RowProperty, 1)
label2.SetValue(Grid.ColumnProperty, 0)
grid1.Children.Add(label2)
Dim tbox1 As New TextBox()
tbox1.SetValue(Grid.RowProperty, 1)
tbox1.SetValue(Grid.ColumnProperty, 1)
Dim binding As New Binding() With {.Source = slider1, .Path = New PropertyPath(Slider.ValueProperty)}
tbox1.SetBinding(TextBox.TextProperty, binding)
grid1.Children.Add(tbox1)
Dim label3 As New Label() With {.Content = "Rectangle:"}
label3.SetValue(Grid.RowProperty, 2)
label3.SetValue(Grid.ColumnProperty, 0)
grid1.Children.Add(label3)
Dim rect1 As New Rectangle() With {.Height = 100.0}
rect1.SetValue(Grid.RowProperty, 2)
rect1.SetValue(Grid.ColumnProperty, 1)
Dim brush As New LinearGradientBrush() With _
{.StartPoint = New Point(0.0, 0.0), .EndPoint = New Point(1.0, 1.0)}
brush.GradientStops.Add(New GradientStop() With {.Color = Colors.Yellow, .Offset = 0.0})
brush.GradientStops.Add(New GradientStop() With {.Color = Colors.Red, .Offset = 1.0})
rect1.Fill = brush
grid1.Children.Add(rect1)
AddHandler rect1.MouseLeftButtonDown, AddressOf rect1_MouseLeftButtonDown
End Sub
Private Sub rect1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
Me.Title = e.GetPosition(grid1).ToString()
End Sub
End Class