Programaticaly在WPF画布上创建按钮

时间:2017-01-29 23:40:42

标签: wpf vb.net canvas

我如何通过使用来自某些来源(即数据库)的画布cooridnates在编辑器上创建按钮

我有画布,我需要填充按钮,我将用于每个循环创建数据库中坐标的按钮。

 <Canvas HorizontalAlignment="Left" Height="308" Margin="10,326,0,0" VerticalAlignment="Top" Width="815">
        <Button Name="Button2" Canvas.ZIndex="2" Content="Button1" Canvas.Top="150" Canvas.Left="130" Width="163" Height="58" FontSize="26" Click="Button2_Click" />
        <Button Name="Button3" Canvas.ZIndex="2" Content="Button1" Canvas.Top="23" Canvas.Left="618" Width="163" Height="58" FontSize="26" Click="Button2_Click" />
    </Canvas>

我可以创建按钮programaticaly但我似乎无法通过VB.NET代码设置所有WPF / Canvas属性

  Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)

Dim n As New Button
    n.Width = 20
    n.Height = 20
    n.Content = "TV"
    canvas1.Children.Add(n)
   End Sub

我如何设置:Canvas.Top,Canvas.ZIndex,Canvas.Left?

1 个答案:

答案 0 :(得分:-1)

好的,找到了解决方案。

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    Dim n As New Button
    n.Width = 200
    n.Height = 200

    ' Set Canvas position 
    Canvas.SetLeft(n, 10)
    Canvas.SetTop(n, 10)
    ' Add Button to Canvas 
    canvas1.Children.Add(n)
End Sub